This kind of questions might seem as one of those geeky considerations, but I rather ask you for thorough explanation of what is going on to satisfy my curiosity.
Let's assume we have a following chunk of code:
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
printf("%d\n", ch);
return 0;
}
After compilation, one can type at the beginning of line simulated EOF with CTRL+Z shortcut and press ENTER - this is done twice.
Output looks like this:
^Z
^Z
-52
Press any key to continue . . .
1) What is happening right now?
I have another doubt concerning such loop:
while (scanf("%c", &ch) != EOF)
printf("%c\n", ch);
printf("BYE!\n");
Output will be:
^Z
^Z
BYE!
Press any key to continue . . .
2) Why is not it terminating after first EOF simulation?
EDIT: I searched for another answers on SO relating to my doubts and I think that it is difficult to use scanf()
, as it has got better substitutes like fgets()
or fread()
. Please take a look on another example below:
int input;
char ch;
while (scanf("%d", &input) != 1) //checking for bad input
while ((ch = getchar()) != '\n') //disposing of bad input
putchar(ch);
printf("BYE!\n");
I input five times CTRL+Z at the beginning of line and the output would become:
^Z
^Z
^Z
^Z
^Z
^Z
^Z
^Z
^Z
^Z
^CPress any key to continue . . .
I added five more EOFs and had to kill program with CTRL+C on the last line.
3) Why the space appeared on the 5-th line and was visible to the end (sometimes two spaces are before '^CPress any key to continue . . .')?
Last example is amendment of loop from above (there is no meaning in code):
while (scanf("%d", &input) != EOF);
printf("BYE!\n");
Output is:
^Z
^Z
^Z
BYE!
Press any key to continue . . .
4) Why are we using three times CTRL+Z instead of two as it was written in above comment?