> If i'm not mistaken, 10 is the numeric value of the newline character (\n).
This is correct.
Welcome to the strange world of scanf - a wonderful function for converting user input, but has way too many traps for the unwary.
The thing you've got to remember about scanf is that it only uses the minimum number of characters possible to satisfy the current conversion requirement (even when it fails).
If this is your scanf call
int res = scanf( "%d", &my_int );
An input buffer of "4\n" would have res=1 (the number of sucessful conversions and assignments), my_int = 4 (the number you typed in), and "\n" left on the input stream.
An input buffer of "Four\n" would result in res=0, my_int unmodified, and "Four\n" still in the input buffer.
Code:
#include <stdio.h>
void flush_input ( FILE *fp ) {
int ch;
while ( (ch=fgetc(fp)) != EOF && ch != '\n' );
}
int main ( ) {
int res, my_int;
printf( "Type in a number > " );
fflush( stdout ); /* the only correct use of fflush is on output streams */
res = scanf( "%d", &my_int );
if ( res == 1 ) {
printf( "You typed in %d\n", my_int );
} else {
printf( "That wasn't a number\n" );
}
printf( "Press return to finish >" );
fflush( stdout );
flush_input( stdin );
getchar();
return 0;
}
The better way of coping with this is to use fgets and sscanf.