我读到 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/