How is conversion of float/double to int handled in printf?
Posted
by Sandip
on Stack Overflow
See other posts from Stack Overflow
or by Sandip
Published on 2010-03-08T01:25:24Z
Indexed on
2010/03/08
1:35 UTC
Read the original article
Hit count: 340
c
Consider this program
int main()
{
float f = 11.22;
double d = 44.55;
int i,j;
i = f; //cast float to int
j = d; //cast double to int
printf("i = %d, j = %d, f = %d, d = %d", i,j,f,d);
//This prints the following:
// i = 11, j = 44, f = -536870912, d = 1076261027
return 0;
}
Can someone explain why the casting from double/float to int works correctly in the first case, and does not work when done in printf?
This program was compiled on gcc-4.1.2 on 32-bit linux machine.
© Stack Overflow or respective owner