Adding to other answers, you can also use strtol to determine if a string has all numbers or not. It basically converts the string to an integer and excludes any non-integer numbers. You can read the man page for more information on this feature and the extensive error checking you can do with it.
In addition, you should use:
scanf("%9s", numbers);
Instead:
scanf("%s", numbers);
To avoid buffer overflow.
Here is a sample code:
#include <stdio.h> #include <stdlib.h> #define MAXNUM 10 #define BASE 10 int main(void) { char numbers[MAXNUM]; char *endptr; int number; printf("Enter string: "); scanf("%9s", numbers); number = strtol(numbers, &endptr, BASE); if (*endptr != '\0' || endptr == numbers) { printf("'%s' contains non-numbers\n", numbers); } else { printf("'%s' gives %d, which has all numbers\n", numbers, number); } return 0; }
Input Example 1:
Enter string: 1234
Output:
'1234' gives 1234, which has all numbers
Input Example 2:
Enter string: 1234hello
Output:
'1234hello' contains non-numbers
source share