#include <stdio.h>
void print_binary(int n);
void test();
int main(){
test();
return 0;
}
void print_binary (int n){
unsigned int mask = 0;
mask = ~mask^(~mask >> 1);
for (; mask != 0; mask >>= 1){
putchar ((n & mask) ? '1' : '0');
}
}
void test(){
int x;
float *p;
p = (float *) &x;
printf ("x init value :%d\n", x);
printf ("addr x and p are %p %p\n", &x, p);
printf ("print x in bit ");
print_binary(x);
printf ("\n");//00000000000000000000000000000000
*p = 6.35;
printf ("print x in bit ");
print_binary(x);
printf ("\n");//01000000110010110011001100110011
printf ("x after 6.35 value :%d\n", x);//1087058739
printf ("call1 x:%.100f\n", x);//0.0000000.....
printf ("x:%d\n", x);//1087058739
printf ("call2 x:%f\n", x);//0.000000
printf ("p:%f\n", *p);//6.350000
printf ("call3 x:%f\n", x);//6.350000
}
Results:
x init value :0
addr x and p are 0x7ffc37d5ba8c 0x7ffc37d5ba8c
print x in bit 00000000000000000000000000000000
print x in bit 01000000110010110011001100110011
x after 6.35 value :1087058739
call1 x:0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
x:1087058739
call2 x:0.000000
p:6.350000
call3 x:6.350000
I print my x
after *p = 6.35;
, and
in the memory, we get 01000000110010110011001100110011
, this is the right number according to the IEEE754,
could someone explain why my first printf ("call1 x:%.100f\n", x)
print me 0.00...
,
but after I printf ("p:%f\n", *p)
, it can print the 6.35
?