I have the following program:
int main(int argc, char *argv[])
{
char ch1, ch2;
printf(\"Input the first character:\"); // Line 1
scanf(\"%c\", &ch
But I cannot explain myself how it works? Because in the while statement, we use
getchar() != '\n'
, that means read any single character except'\n'
?? if so, in the input buffer still remains the'\n'
character??? Am I misunderstanding something??
The thing you may not realize is that the comparison happens after getchar()
removes the character from the input buffer. So when you reach the '\n'
, it is consumed and then you break out of the loop.