too many new and intermediate users of C attempt to use the command
fflush(stdin) to "flush the input buffer". this is patently wrong.
the rule is (and click if you don't believe me) : NEVER USE "FFLUSH()" ON INPUT STREAMS SUCH AS "STDIN"
here is one method of properly flushing extra (and unwanted) characters from the stdin input stream. the code is written as a macro, and requires a character array to collect excess (junk) input. the macro will also ensure the newline character is stripped.
the intended use is in conjunction with fgets. for example:
char userInput; // change array size to any arbitrary value printf("input a string and hit <enter>. A maximum of %d characters will be recorded.\n",sizeof(userInput)-1); fgets(userInput,sizeof(userInput),stdin); FLUSH_STDIN(userInput); // strip newline, flush extra chars printf("the first %d characters are \"%s\", any others have been discarded.\n",strlen(userInput),userInput);