There is strtol
which is better IMO. Also I have taken a liking in strtonum
, so use it if you have it (but remember it’s not portable):
long long strtonum(const char *nptr, long long minval, long long maxval, const char **errstr);
You might also be interested in strtoumax
and strtoimax
which are standard functions in 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
:
The call atoi(str) shall be equivalent to:
(int) strtol(str, (char **)NULL, 10)
except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.