Reputation: 4368
Why doesn't this code do anything when i execute it? It's supposed to count characters i even pressed ctrl+z which someone suggested and it still wont print how many characters after i entered random things. I'm using windows btw
#include <stdio.h>
int main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
return 0;
}
Upvotes: 0
Views: 141
Reputation: 153
You can use ^D to provide EOF signal to your program. Many time ^Z doesn't works.
Upvotes: 1
Reputation: 581
try calling your.exe < your_input.file
or just press Ctrl+Z
and then Enter
to get EOF
Upvotes: 0
Reputation: 2957
Is your sample compiled with UNICODE=1? You have to compare against WEOF then.
Upvotes: 1
Reputation: 1569
because while getchar() is an infinite loop.
So you never exit the loop untile you kill the process.
Upvotes: -2
Reputation: 22031
It works if you press CTRL+Z
and then enter. This triggers EOF
.
If you want it to end when you press enter, use
while( getchar() != '\n' )
Upvotes: 2