If you don't want to use scanf, you have a couple of choices:
You can use a combination of fgets and strtol (for integer inputs) or strtod (for floating point inputs). Example (untested):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
...
char buf[12]; // 10 digits, plus sign, plus terminator
char *chk; // points to first character *not* converted by strtol
if ( !fgets( buf, sizeof buf, stdin ) )
{
fprintf( stderr, "Error on input\n" );
return EXIT_FAILURE;
}
long value = strtol( buf, &chk, 10 ); // 10 means we expect decimal input
if ( !isspace( *chk ) && *chk != 0 )
{
fprintf( stderr, "Found non-decimal character %c in %s\n", *chk, buf );
fprintf( stderr, "value may have been truncated\n" );
}
printf( "Input value is %ld\n", value );
You can use getchar to read individual characters, use isdigit to check each, and build the value manually. Example (also untested):
#include <stdio.h>
#include <ctype.h>
...
int value = 0;
for ( int c = getchar(); isdigit( c ); c = getchar() ) // again, assumes decimal input
{
value *= 10;
value += c - '0';
}
printf( "Value is %d\n", value );