There is strtol
which is better IMO.
(有strtol
是更好IMO。)
Also I have taken a liking in strtonum
, so use it if you have it (but remember it's not portable): (我也很喜欢strtonum
,所以如果有的话就用它(但要记住它不是便携式的):)
long long
strtonum(const char *nptr, long long minval, long long maxval,
const char **errstr);
EDIT (编辑)
You might also be interested in strtoumax
and strtoimax
which are standard functions in C99.
(您可能也对strtoumax
和strtoimax
感兴趣,它们是C99中的标准功能。)
For example you could say: (例如,您可以说:)
uintmax_t num = strtoumax(s, NULL, 10);
if (num == UINTMAX_MAX && errno == ERANGE)
/* Could not convert. */
Anyway, stay away from atoi
:
(无论如何,请远离atoi
:)
The call atoi(str) shall be equivalent to:
(调用atoi(str)应等效于:)
(int) strtol(str, (char **)NULL, 10)
except that the handling of errors may differ.
(除了错误处理可能有所不同。)
If the value cannot be represented, the behavior is undefined . (如果无法表示该值,则行为是undefined 。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…