I'd like to gain a little better understanding of how things work in the background in regards to this command.
I started off with trying to prevent my application from failing when scanf()
received a char
instead of the int
it was expecting. The solution provided to me was to add the line while (getchar() != '\n');
to clear the input buffer.
And it worked. Great!
But since I was otherwise using scanf()
and not getchar()
I found it a bit confusing that I use a completely different command for input to clear the buffer.
I have been Googling it a bit and from what I can tell, getchar()
uses stdin
and that is the buffer that is being cleared with that command. That would by extension also mean that scanf()
is also using stdin
in that case.
Is that assumption correct?