这是我在 Mac OS X(10.8.1、64 位和 10.6.8、32 位)和 Ubuntu(10.04、32 位)上运行的代码:
printf("%d\n", atoi("2147483648"));
// returns -2147483648 in Mac OS X 10.8.1 and 10.6.8
// returns 2147483647 in Ubuntu
这是我在谷歌搜索后发现的:http://gynvael.coldwind.pl/?id=365
是否有任何原因导致不同系统的输出不同?
最佳答案
这并不奇怪,因为行为 is not specified按照标准:
There is no standard specification on what happens when the converted value would be out of the range of representable values by an int.
strtol
函数在返回中为您提供更多信息,因为当值不适合 long int
时,它将 errno
设置为 ERANGE
。
关于c - 在 Mac OS X 和 Ubuntu 中对 C 使用 atoi() 时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12285342/