Re: printf("%d",float)
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 06:59:16 +0530
candy_init@xxxxxxxxx wrote:
Hi all,
I just came across the following program:
#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n", a);
printf expects and treats the corresponding argument as a signed int,
while you've passed it a float value.
printf("%d\n", *(int *)&a);
Same as above and an additional problem:
Pointers values of different types need not be compatible in C, except
that a conversion of any pointer to void * and back again shall be
okay.
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?
Yes. You've written an incorrect program, so incorrect results are to be
expected.
Read:
<http://www.c-faq.com/>
.
- Follow-Ups:
- Re: printf("%d",float)
- From: Keith Thompson
- Re: printf("%d",float)
- References:
- printf("%d",float)
- From: candy_init
- printf("%d",float)
- Prev by Date: Re: how to optimize a for loop
- Next by Date: Re: how to optimize a for loop
- Previous by thread: Re: printf("%d",float)
- Next by thread: Re: printf("%d",float)
- Index(es):
Relevant Pages
|