Search code examples
ceofkernighan-and-ritchie

how does this code from "The C Programming Language" work?


I'm reading "The C Programming Language (2nd ed.) and near the beginning, it has examples like this:

while((c = getchar()) != EOF)
    if(c == '\n'){
        ++n1;

I can see how this would work while reading from a file, and I understand this syntax... But this is just reading from the console--how does one signal end of file when entering characters from a console? I'm using Windows XP... MinGW compiler... Anyways, was this book written for waaay earlier systems with like an EOF button or something?

Update

well, I have one more question, that's just related to how the end-of-file works under Windows.

If I just while(getchar()!=EOF);, then I can just keep typing characters until I signal EOF via ^Z. But, I have to write a newline, then hit ^Z, then another newline... Why does it have to be on its own line?


Solution

  • Windows uses Ctrl-Z for EOF, and UNIX uses Ctrl-D. See http://bytes.com/groups/c/217873-eof-windows , and excellent book choice. :)