there are many questions about flushing stdin, mine is not about how to flush it, rather about the meaning of the following function:
while((c = getchar()) != '\n' && c != EOF);
I am trying to think of scenarios why and how we would need EOF in this function for emptying stdin? The user types something and presses enter, and we empty the buffer until we find enter, where does EOF come in?
Thank you
Edit: Thank you very much for the first answer and the second comment, that answers my question and totally satisfies me.
Other than the scenario where we pipe our input from a file where we have a real EOF, which pretty much answered my question, I still do not understand why we would need EOF in any other scenario for a function like this. the user can only invoke EOF at the beginning of a line in the terminal, so it cannot be used for emptying the buffer.
stdin
withEOF
, that code is just reading all the characters fromstdin
and discards them, it depends on where that code is, I mean the context. If you for example usescanf()
to read a value and input a valid value followed by spaces, that code will remove all the spaces from the input buffer. – Iharob Al Asimic
which should beint
and notchar
. – chux - Reinstate Monica