strtol is standard, along with strtoul, strtoll (C99), strtoull (C99), strtof, strtod and strtold. All of the ato* functions are based off of the strto* functions, but they lack the extra arguments and error checking. Here's an example of strtol's extensive error handling capabilities:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h> /* for strtol */
void main()
{
char line[1024];
int rc = scanf("%1023[^\n]%*[^\n]", line);
if (rc != EOF)
getchar(); /* Eat the newline */
if (rc == 1) {
char *end;
long value;
/* strtol sets errno to ERANGE if the value is too large for long int */
errno = 0;
/*
The second argument points to the character where conversion stopped.
The third argument is the radix for conversion, where 0 == 8, 10 and 16.
*/
value = strtol(line, &end, 0);
/* Check errno first because any library call can change it */
if (errno == ERANGE)
fputs("Value is out of range for type long\n", stderr);
else if (end == line)
fputs("Cannot extract an integer value\n", stderr);
else {
if (*end != '\0')
printf("Unconverted: \"%s\"\n", end);
printf("The value is %ld\n", value);
}
}
}