Reputation: 997
so I built a very basic program according to The C Programming Language book, but when I run it, it keeps asking input after I enter one, the loop should ended when there are no inputs longer right? or Am I wrong? srry for my bad english
int main()
{
long nc;
nc = 0;
while ( getchar() != EOF) {
++nc;
}
printf("%ld\n", nc);
}
Upvotes: 0
Views: 746
Reputation: 1
You can press the Ctrl + D to input EOF
, anything other keyboard can't! So if you want to interrupt the loop, you must input the EOF
.
Upvotes: 0
Reputation: 126488
This will keep reading input until it gets to an end-of-file. If you're reading from the terminal (you haven't redirected the program to read from a file), it will only get an EOF if you explicitly give it one. How you do that dpends on your OS, but on most UNIX-like systems, you can generate an explicit eof by hitting ctrl-D
Upvotes: 0
Reputation: 2747
No.
For standard input, you must input EOF manually. It's Ctrl+Z in Windows and Ctrl+D in Linux.
If you are using Linux and redirect standard input from file. It will end.
Upvotes: 1
Reputation: 47840
Your loop is expecting an EOF character to terminate, not just an empty string. *nix consoles typically translate a Ctrl-D
on an empty line as EOF, in Windows I believe it's Ctrl-Z
but I could be wrong.
Upvotes: 2