C语言 atol() v/s。strtol()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3792663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
atol() v/s. strtol()
提问by Eli Bendersky
What is the difference between atol() & strtol()?
atol() 和 strtol() 有什么区别?
According to their man pages, they seem to have the same effect as well as matching arguments:
根据他们的手册页,它们似乎具有相同的效果以及匹配的参数:
long atol(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
In a generalized case, when I don't want to use the baseargument (I just have decimal numbers), which function should I use?
在一般情况下,当我不想使用base参数(我只有十进制数)时,我应该使用哪个函数?
回答by Eli Bendersky
strtolprovides you with more flexibility, as it can actually tell you if the whole string was converted to an integer or not. atol, when unable to convert the string to a number (like in atol("help")), returns 0, which is indistinguishable from atol("0"):
strtol为您提供更大的灵活性,因为它实际上可以告诉您整个字符串是否已转换为整数。atol, 当无法将字符串转换为数字(如 in atol("help"))时,返回 0,这与 无法区分atol("0"):
int main()
{
int res_help = atol("help");
int res_zero = atol("0");
printf("Got from help: %d, from zero: %d\n", res_help, res_zero);
return 0;
}
Outputs:
输出:
Got from help: 0, from zero: 0
strtolwill specify, using its endptrargument, where the conversion failed.
strtol将使用其endptr参数指定转换失败的位置。
int main()
{
char* end;
int res_help = strtol("help", &end, 10);
if (!*end)
printf("Converted successfully\n");
else
printf("Conversion error, non-convertible part: %s", end);
return 0;
}
Outputs:
输出:
Conversion error, non-convertible part: help
Therefore, for any serious programming, I definitely recommend using strtol. It's a bit more tricky to use but this has a good reason, as I explained above.
因此,对于任何严肃的编程,我绝对推荐使用strtol. 使用起来有点棘手,但这有一个很好的理由,正如我上面所解释的。
atolmay be suitable only for very simple and controlled cases.
atol可能只适用于非常简单和受控的情况。
回答by AnT
atolfunctionality is a subset of strtolfunctionality, except that atolprovides you with no usable error handling capabilities. The most prominent problem with ato...functions is that they lead to undefined behavior in case of overflow. Note: this is not just a lack of informative feedback in case of an error, this is undefined behavior, i.e. generally an unrecoverable failure.
atol功能是功能的一个子集strtol,除了不atol提供任何可用的错误处理功能。ato...函数最突出的问题是它们在溢出的情况下会导致未定义的行为。注意:这不仅仅是在发生错误时缺乏信息反馈,这是未定义的行为,即通常是不可恢复的故障。
This means that atolfunction (as well as all other ato..functions) is pretty much useless for any serious practical purposes. It was a design mistake and its place is on the junkyard of C history. You should use functions from strto...group to perform the conversions. They were introduced, among other things, to correct the problems inherent in functions of ato...group.
这意味着atol函数(以及所有其他ato..函数)对于任何严肃的实际目的几乎没有用处。这是一个设计错误,它的位置在 C 历史的垃圾场上。您应该使用strto...group 中的函数来执行转换。除其他外,引入它们是为了纠正ato...群体功能中固有的问题。
回答by jfmercer
According to the atoiman page, it has been deprecated by strtol.
根据atoi手册页,它已被strtol.
IMPLEMENTATION NOTES
The atoi() and atoi_l() functions have been deprecated by strtol() and strtol_l()
and should not be used in new code.
回答by schot
In new code I would always use strtol. It has error handling and the endptrargument allows you to see which part of the string was used.
在新代码中,我将始终使用strtol. 它具有错误处理功能,endptr参数允许您查看使用了字符串的哪一部分。
The C99 standard states about the ato*functions:
C99 标准规定了有关ato*功能:
Except for the behavior on error,they equivalent to
atoi: (int)strtol(nptr,(char **)NULL, 10)atol: strtol(nptr,(char **)NULL, 10)atoll: strtoll(nptr, (char **)NULL, 10)
除了错误行为外,它们等价于
atoi: (int)strtol(nptr,(char **)NULL, 10)atol: strtol(nptr,(char **)NULL, 10)atoll: strtoll(nptr, (char **)NULL, 10)
回答by Andrew Stein
atol(str)is equivalent to
atol(str)相当于
strtol(str, (char **)NULL, 10);
Use strtol if you want the end pointer (to check whether there are more characters to read or if in fact you have read any at all) or a base other than 10. Otherwise, atol is fine.
如果您想要结束指针(检查是否有更多字符要读取,或者实际上您是否已读取任何字符)或 10 以外的基数,请使用 strtol。否则,atol 就可以了。
回答by Jeff Mercado
If memory serves, strtol()has the added benefit to set the (optional) endptrto point to the first character that could not be converted. If NULL, it is ignored. That way if you're processing a string containing numbers and characters mixed, you could continue.
如果没记错的话,strtol()设置(可选)endptr指向第一个无法转换的字符有额外的好处。如果NULL,则忽略。这样,如果您正在处理包含数字和字符混合的字符串,则可以继续。
e.g.,
例如,
char buf[] = "213982 and the rest";
char *theRest;
long int num = strtol(buf, &theRest, 10);
printf("%ld\n", num); /* 213982 */
printf("%s\n", theRest); /* " and the rest" */
回答by Shriram V
The man page of strtol gives the following:
strtol 的手册页给出了以下内容:
ERRORS
EINVAL (not in C99) The given base contains an unsupported value.
ERANGE The resulting value was out of range.
The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).
The following code checks for range errors. (Modified Eli's code a bit)
以下代码检查范围错误。(稍微修改了 Eli 的代码)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main()
{
errno = 0;
char* end = 0;
long res = strtol("83459299999999999K997", &end, 10);
if(errno != 0)
{
printf("Conversion error, %s\n", strerror(errno));
}
else if (*end)
{
printf("Converted partially: %i, non-convertible part: %s\n", res, end);
}
else
{
printf("Converted successfully: %i\n", res);
}
return 0;
}

