The following code snippet has been taken from Teach Yourself C by Herbert Schildt page 234.
#include <stdio.h>
int main(void)
{
char ch;
do
{
ch = getchar(); //suppose asdf is input
putchar('.');
} while (ch != '\n');
return 0;
}
Which returns,
asdf
.....
Process returned 0 (0x0) execution time : 0.050 s
Press any key to continue.
After this snippet Herbert Schildt writes, "Instead of printing a period between each character, what you will see on the screen is all the letters you typed before pressing ENTER, followed by a string of periods."
I am stuck with this snippet for a while
. I tried some variations of the snippet to understand it better, which raised more questions than answering. This is actually running against my current understanding. Rather than printing out the above output, my current understanding suggests the following output:
asdf //input asdf
.
asd //input asd
.
\n //hit ENTER
.
Process returned 0 (0x0) execution time : 0.050 s
Press any key to continue.
With the above introduction, I have the following questions:
- How the above snippet is looping even?
- What am I wrong with my current intuition?