I'm reading/practicing with this book about C language: "C Programming - A Modern Approach 2" and I stumbled upon this piece of code whose explanation is odd to me:
Since scanf doesn't normally skip white spaces, it's easy to detect the end of an input line: check to see if the character just read is the new-line character. For example, the following loop will read and ignore all remaining characters in the current input line:
do {
scanf("%c", &ch);
} while (ch != '\n');
When scanf is called the next time, it will read the first character on the next input line.
I understand the functioning of the code (it exits the loop when it detects enter or '\n'
) but not the lesson(?).
What is the purpose of this, since to store the input from the user into &ch
you HAVE to press the enter key (quitting the loop)?
Also what does "the following loop will read and ignore all remaining characters in the current input line" mean actually?