There is also strtol.
It has three arguments: the string to test, a pointer to pointer to char and the base of the number.
After the conversion, the second argument will point to the first character which couldn't be interpreted as a valid digit. If no conversion was done (no valid number) it points to the beginning of the string.
If you want to accept inputs like "100foo" to be interpreted as "100" you would test if the second argument points to the beginning:
Code:
int nr;
char *end_ptr;
nr = (int) strtol(argv[1], &end_ptr, 10);
if (end_ptr == argv[1])
printf("Not a valid number!\n");
If you want to accept only valid numbers (all characters in the string must be consumed) you would test if the second argument points to the end of the string ('\0'):
Code:
int nr;
char *end_ptr;
nr = (int) strtol(argv[1], &end_ptr, 10);
if (*end_ptr != '\0')
printf("Not a valid number!\n");
HTH, Andreas