Re: printf("%d",float)
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 31 Oct 2007 01:15:33 +0000
candy_init@xxxxxxxxx said:
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?
You are wrong in thinking that %d is the appropriate format specifier for a
float, and you are wrong in thinking that you can meaningfully cast the
address of a float into the address of an int.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: printf("%d",float)
- From: Jack Klein
- Re: printf("%d",float)
- References:
- printf("%d",float)
- From: candy_init
- printf("%d",float)
- Prev by Date: how to optimize a for loop
- Next by Date: Re: Please help me to find the error
- Previous by thread: printf("%d",float)
- Next by thread: Re: printf("%d",float)
- Index(es):
Relevant Pages
|