-
Explaination of EOF
I was reading the FAQ, "How do I get my program to wait for a key press"
If I understand the example correctly, (and please correct me if I'm wrong) the program gets input until it recives a newline when it then returns 0, but what about the section of code that says "ch != EOF'"
heres the example:
Code:
#include <stdio.h>
int main(void)
{
int ch;
printf ("Press [Enter] to continue");
while ((ch = getchar()) != '\n' && ch != EOF);
return(0);
}
Beginner C programmer says:
Thanks in advance
-
EOF is End Of File
Like you would get if you did
myprog < some_file_with_no_newlines
But if you're just using the normal stdin (the keyboard), you generate an EOF by pressing either ctrl-z (DOS/Win) or ctrl-d (Unix/Linux)
EOF is the only value which is guaranteed to show up at some point, so you must check for it.
Otherwise, your loop could become an infinite loop.