Alternative to atoi()
there is better way with using strtol()
:
Defined in header <stdlib.h>
long strtol( const char *str, char **str_end, int base ); (until C99)
long strtol( const char *restrict str, char **restrict str_end, int base ); (since C99)
long long strtoll( const char *restrict str, char **restrict str_end, int base ); (since C99)
The call atoi(str)
shall be equivalent to: (int) strtol(str, (char **)NULL, 10)
except that the handling of errors may differ.
For atoi()
, if the value cannot be represented(not in the format below), the behavior is undefined.