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.
How are you ending your input and what system are you on?
If you are hitting an 'interrupt' or 'kill' control key combination then it's likely that you are killing your process before it can print.
If you use something like Ctrl-D on unix or Ctrl-Z at the start of a line on windows then this will signal 'end of input' without killing the process.
You can also try redirecting your input from a test file. e.g.:
EOF Working
Write your inputs and press Enter then ctrl+z for Windows and ctrl+d for Unix then press Enter. Same you can see in Image i attached. It will definitely.
getchar() returns a value from standard in (typically the keyboard). I don't remember what character EOF will map to, but you probably can't type it.
You will only get an
EOF
from the stream when the end of file is reached, not the end of line. How you signal an end of file depends on your OS and terminal settings.It's usually CTRLd on UNIX-type systems and CTRLz on Windows. For UNIX in cooked mode (normal input mode), you'll generally have to enter it as the first character of a line and follow it with a newline (ENTER).
With Windows, the CTRLz can be entered anywhere on the line, but still needs to be followed by a newline.
In UNIX, the actual character to inform the terminal interface that you want to send
EOF
can be set with thestty
command. If you executestty -a
, you'll see something like:You can see at the end of the second line that
eof
is set to^D
(CTRLd). You can change this with:to set it to CTRLx, for example. You can also set a huge number of other things, most of which will make your current terminal unusable, so be careful :-)
Bottom line, if you want to signal your program that the file is finished, use CTRLd for UNIX (or check
stty
if that doesn't work) or CTRLz for Windows. If you want to just get a line of input, use the\n
character in your code as follows: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.On Windows either a CTRL-Z or F6 will signal the end of a file.