Re: printf("%d",float)
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 30 Oct 2007 19:23:58 -0700
santosh <santosh.k83@xxxxxxxxx> writes:
candy_init@xxxxxxxxx wrote:[...]
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:
Not quite. The argument is of type int, so "%d" is the correct
format. The problem, of course, is that obtaining the value of that
argument (evaluating ``*(int *)&a'') invokes undefined behavior.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- printf("%d",float)
- From: candy_init
- Re: printf("%d",float)
- From: santosh
- printf("%d",float)
- Prev by Date: Re: how to optimize a for loop
- Next by Date: Re: printf("%d",float)
- Previous by thread: Re: printf("%d",float)
- Next by thread: Re: printf("%d",float)
- Index(es):
Relevant Pages
|