c - atoi() — 字符串到整数

标签 c

我读到 atoi() 已被弃用,它等同于:

(int)strtol(token_start, (char **)NULL, 10);

这是否意味着我应该使用上面的而不是 atoi(chr) 还是只是说它们是等价的?

最佳答案

atoi 未弃用,您的来源不正确。当前的 C 标准 ISO 9899:2011 中没有任何内容表明这一点(例如,参见第 6.11 章 future 语言方向),早期标准中也没有任何内容。

根据 C 标准,atoi 等同于 strtol,如下所示,C11 7.22.1.2:

The atoi, atol, and atoll functions convert the initial portion of the string pointed to by nptr to int, long int, and long long int representation, respectively.

Except for the behavior on error, they are equivalent to

atoi: (int)strtol(nptr, (char **)NULL, 10)

atol: strtol(nptr, (char **)NULL, 10)

atoll: strtoll(nptr, (char **)NULL, 10)

strtol 是首选,因为 atoi 在错误时调用未定义的行为。见 7.22.1“如果无法表示结果的值,则 行为未定义。”

关于c - atoi() — 字符串到整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488569/

相关文章:

c - strerror_r 缓冲区总是空终止

c - C 中 "* "、 "* "和 "*"指针之间的区别

c - 即使在编译后也会出现段错误?

c - 为什么下面的 C 程序会报总线错误?

带 print 的 CMake 构建后输出命令

c - 将 block_device 映射到设备结构

c++ - PostgreSQL 的 libpq : Encoding for binary transport of ARRAY[]-data?

将 char* 转换为字符串,然后分配给结构成员 : initialization makes integer from pointer

c - fallocate 和 ftruncate 有什么区别

c - Linux 串行 IO - 在两个线程之间拆分 Tx 和 Rx?