Posted By: Anonymous
I am trying to find out if there is an alternative way of converting string to integer in C.
I regularly pattern the following in my code.
char s = "45"; int num = atoi(s);
So, is there a better way or another way?
long long strtonum(const char *nptr, long long minval, long long maxval, const char **errstr);
You might also be interested in
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
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.