Hello. Disclaimer: this is a vexed question. At least, let me tell you that:
- I know that EOF is the value that getchar() returns when end-of-file is reached;
- It is not a character;
- It's generally "-1" but it cannot be expressed typing "-1".
- I defined the variable storing each character as int, not char.
So, I copied and pasted this program from the Kernighan-Ritchie book:
Afer this example, the authors go on to say:
/* copy input to output; 2nd version */
while ((c = getchar()) != EOF)
"The while gets a character, assigns it to c, and then tests whether the character was the end-of-file signal. If it was not, the body of the while is executed, printing the character. The while then repeats. When the end of the input is finally reached, the while terminates and so does main."
This gives me to understand that, if I enter the text stream "Hello" and press Enter ("Hello\n"), getchar() will go on to read each character of the stream, which, as I see it, should be "H", "e", "l", "l", "o", "\n". Then getchar() has nothing else to read and... returns EOF? That's what I get from that paragraph, but apparently you're stuck in an almost infinite loop unless you enter the actual value of EOF yourself using a keyboard combination.
Is this what's supposed to happen? If so, what's the point of all the examples using EOF, when the authors don't remotely imply that the only way getchar() is going to return EOF is if you feed EOF to it? I thought it automatically returned it when it had no more characters to read from one text stream. The program above goes on to read countless text streams.