Re: printf("%d",float)
- From: ming li <limingpy@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 19:14:19 -0700
On 10 31 , 8 48 , candy_i...@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("%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?
hi, I help this test program will help you!
#include <stdarg.h>
#include <stdio.h>
// printf's action is like this
void test(char *fmt, ...)
{
int i;
float j;
va_list ap, app;
va_start(ap, fmt);
app = ap;
i = va_arg(ap, int); // when you call like printf("%d\n", i);
this line while be called
j = va_arg(app, double); // when you call like printf("%f\n", i);
this line while be called
// and you can't call it like j = va_arg(app,
float), because gcc compiler will complain
va_end(ap);
printf("if the args interpret as int, you get %d\n", i);
printf("it should interpret as double, the right answer is %f\n",
j);
}
int main(void)
{
float a = 12.5;
test("%d", a);
return 0;
}
below is define of va_arg, you can find it in <stdarg.h>
typedef char * va_list;
#define va_start _crt_va_start
+-- #define va_arg _crt_va_arg
| #define va_end _crt_va_end
|
| #define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) +
_INTSIZEOF(v) )
+-> #define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) -
_INTSIZEOF(t)) )
#define _crt_va_end(ap) ( ap = (va_list)0 )
.
- 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
|