c - 在 Mac OS X 和 Ubuntu 中对 C 使用 atoi() 时的不同结果

标签 c glibc atoi

这是我在 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/

相关文章:

c - 我可以假设 atoi() 出错时的行为是什么?

使用 atoi 时无法将 char 转换为 int

c - 为 qsort 编写比较函数

c - 在 IOCP 中发送数据的最佳方法是什么?

ubuntu - 构建 glibc 时未定义对 _dl_cet_check 的引用

Ubuntu 找不到 glibc2.14

c - atoi在c程序中的用法

c - 将 GTK 窗口放在屏幕的一角

c - Unix 系统调用名称后面的 (2) 是什么意思?

c - gcc的目录结构