>Of course...it would be better just #include <ctype.h> and then replace
Of course, it would be even better to go all out and perform a stronger check:
Code:
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_integer ( const char *s )
{
char *end;
long result;
errno = 0;
result = strtol ( s, &end, 0 );
/* Incomplete conversion */
if ( *s != '\0' && *end != '\0' )
return 0;
/* Overflow/Underflow */
if ( ( result == LONG_MAX || result == LONG_MIN ) && errno == ERANGE )
return 0;
/* Out of range */
if ( result > INT_MAX || INT_MIN > result )
return 0;
return 1;
}
int main ( void )
{
char buffer[BUFSIZ];
while ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
buffer[strcspn ( buffer, "\n" )] = '\0';
printf ( "%s -- %d\n", buffer, is_integer ( buffer ) );
}
return 0;
}