strtoull和长期算术

任何人都可以解释这个程序的输出以及我如何解决它?

unsigned long long ns = strtoull("123110724001300", (char **)NULL, 10); fprintf(stderr, "%llu\n", ns); // 18446744073490980372 

你有吗?

如果我省略我可以在MacOS X上重现。

 #include  #include  int main(void) { unsigned long long ns = strtoll("123110724001300", (char **)NULL, 10); printf("%llu\n", ns); return(0); } 

省略标题,我得到你的结果。 包括标题,我得到正确的答案。

32位和64位编译。


如注释中所述,在没有strtoll()声明的情况下,编译器将其视为返回int的函数。

要查看更多内容,请查看hex输出:

  123110724001300 0x00006FF7_F2F8DE14 Correct 18446744073490980372 0xFFFFFFFF_F2F8DE14 Incorrect 

手动插入下划线……

如果你想要一个unsigned long long,为什么不使用strtoull呢?

我无法解释这种行为。 但是,在32位Windows XP上使用Cygwin gcc-4.3.2

 #include  #include  int main(void) { unsigned long long ns = strtoull("123110724001300", NULL, 10); fprintf(stderr, "%llu\n", ns); return 0; } 

版画

 E:\ Home> t.exe
 123110724001300