Hi All,
I am aware that this question has been asked before and I have also tried the solution suggested but I am still not able to figure it out why I am getting some wierd results when I am using printf with a 64 bit signed and unsigned integer in C.
I am running Suse 10.2 on a Intel(R) Pentium(R) M processor 2 GHz.
I tried to call functions strtol strtoll strtoul and strtoull with different integer sizes and these are the results i get from my program shown below:
int i;
char str[100];
char endstr[100];
int64_t ret_val = 0LL;
int32_t retval = 0;
uint64_t uret_val = 0UL;
uint32_t uretval = 0;
strcpy(str, "11221212122121 final string");
strcpy(endstr, "");
ret_val = strtoll(str, &endstr, 3);
printf("64bit Size:%d Returned Val strtoll:%016x %lld Endptr:%s\n",sizeof(int64_t), ret_val, ret_val, endstr);
//ret_val = __INT64_C(28346590832469346);
//printf("%lld\n",ret_val);
strcpy(str, "11221212122121 final string");
strcpy(endstr, "");
retval = strtol(str, &endstr, 3);
printf("32bit Size:%d Returned Val strtol:%08x %ld Endptr:%s\n",sizeof(int32_t), retval, retval, endstr);
strcpy(str, "47483649");
strcpy(endstr, "");
uret_val = strtoull(str, &endstr, 0);
printf("64bit Size:%d Returned Val strtoull:%016x %llu \n",sizeof(uint64_t), uret_val, uret_val);
strcpy(str, "47483649");
strcpy(endstr, "");
uretval = strtoul(str, &endstr, 0);
printf("32bit Size:%d Returned Val strtoul:%08x %lu Endptr:%s\n",sizeof(uint32_t), uretval, uretval, endstr);
The results i get from this piece of code is shown below:
64bit Size:8 Returned Val strtoll:0000000000283549(HEX) 11317586717310976 Endptr
null)
32bit Size:4 Returned Val strtol:00283549(HEX) 2635081 Endptr:�V����
64bit Size:8 Returned Val strtoull:0000000002d48b01(HEX) 203940719549743104
32bit Size:4 Returned Val strtoul:02d48b01(HEX) 47483649 Endptr:�V����
What I cannot figure out is why the printf %lld and %llu for int64_t and uint64_t always give some wierd values as the result. For both variants of the strtol,strtoll and strtoul,strtoull functions the HEX value returned is the same as expected whereas the decimal value for strtoll and strtoull is completely different. I am not able to figure out the reason behind this. I am sure there is a logical explanation to the problem but I am just not able to see it. Have googled on this almost the whole day now. I am using the gcc 4.2.1 compiler on Suse 10.2 Linux distribution.
Thanks a million
cheers
vinay