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, wh
There is no portable way of doing this, since the standard input is "blocking" by default you can't read from it in order to throw the input away without causing a block if there was no input.
If you have it, you can use select() to detect that there is input, and then do a read to discard it. I would recommend doing a "large" block read in that case, not a single character at a time.
Please note that getchar() returns int, the constant EOF
does not fit in a character. It's an example of "out of band" communication, it's not a character.