Subject: question about the casting in printf
To: None <firstname.lastname@example.org>
From: YT <email@example.com>
Date: 02/08/2007 02:04:22
The variable argument list handling in stdarg.h of MyLIB is defined as below.
typedef char * va_list;
#define __va_size(type) \
(((sizeof(type) + sizeof(long) - 1) / sizeof(long)) * sizeof(long))
#define va_start(ap, last) ( ap = (va_list)&last + __va_size(last) )
#define va_arg(ap, type) ((type *)(ap += sizeof(type)))[-1]
I have a question about the casting in printf. Below is the example about the
unsigned long long ull = 0x0000000100000003; //MyLIB FreeBSD Linux
printf(“%Lu”, ull); //4294967299 4294967299
printf(“%lu”, ull); //1 3 3
printf(“%lu”, (unsigned long)ull); //3 3 3
"ull" is a 8-byte value, if we want print it in unsigned long form, we will
get "0x00000001" without the casting first, "0x00000003" if casting first.
I think the behavior is correct since argument type in va_arg() is unsigned long
(because of "%lu"), it only handles first 4 bytes, so we will get "0x00000001"
without casting first.
But why I can get "0x00000003" on FreeBSD/Linux platform!?? I don't understand.
Hope someone can solve my doubt. Thanks!