Clearing out stdin in C when it may or may not be empty
I am a programming student looking for a way to get rid of characters that may be hanging around in stdin. I have tried a technique that has been given here in various forms, where you do something like this: void clearStdIn(void) { char c; while((c = getchar()) != '\n' && c != EOF) /* discard */ ; } The problem seems to be that if nothing is in stdin to begin with, this function sits around waiting for the user to hit enter before control flow can move on. What should I do? Flushing an input stream (in a portable way) without blocking could be done like this: #include <stdlib.h> #include