I have a small snippet of code below that I'm running using PellesC.
When the code is executed and I've typed a few characters into the console, I press enter.
Can you explain to me why the printf("%ld\n", nc);
line doesn't seem to get executed? As no output is written to the console.
#include <stdio.h>
int main(void)
{
long nc = 0;
while(getchar() != EOF)
{
++nc;
}
printf("%ld\n", nc);
}
I've decided to learn C thoroughly using the K&R book and I'm embarrassed to say this rather elementary example has me stumped.
Pressing enter doesn't actually cause an EOF
("end of file"). You have to signal that you are completely finished with providing input; on Unix you typically do that by pressing CtrlD. On Windows I believe it's CtrlZ followed by enter, but I'm really not sure about that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With