printf("%d",float)



Hi all,
I just came across the following program:

#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n", a);
printf("%d\n", *(int *)&a);
return 0;
}

The program prints 0 and 1095237362. However, in my opinion it should
print 12 at both the places. Can anybody tell me where I am wrong?

.



Relevant Pages

  • Re: printf("%d",float)
    ... The program prints 0 and 1095237362. ... float, and you are wrong in thinking that you can meaningfully cast the ... address of a float into the address of an int. ... The result of casting a pointer to an object or incomplete to ...
    (comp.lang.c)
  • Re: printf("%d",float)
    ... The program prints 0 and 1095237362. ... float, and you are wrong in thinking that you can meaningfully cast the ... address of a float into the address of an int. ... The result of casting a pointer to an object or incomplete to ...
    (comp.lang.c)
  • Re: printf("%d",float)
    ... printf expects and treats the corresponding argument as a signed int, ... while you've passed it a float value. ... The program prints 0 and 1095237362. ... You've written an incorrect program, so incorrect results are to be ...
    (comp.lang.c)
  • Re: printf("%d",float)
    ... int main ... float a = 12.5; ... The program prints 0 and 1095237362. ... printf("if the args interpret as int, you get %d\n", i); ...
    (comp.lang.c)
  • Re: Is i = i++; bad style?
    ... Don't you think the following program prints 11 on any or your system? ... int main ... I know I'm feeding the troll. ... both a Windows system and a Mac (MPW, PPC, ...
    (comp.lang.c)