First, and code you post should be wrapped in
... your code here ...
tags. That will make sure the forum does not see coding syntax as forum formatting meta characters, and it will preserve your indentation.
As the thread you posted suggests, there is no EOF character.
However, at the lowest level the read() system call (somewhere under getchar()), can be told there is no more input, and it will return 0, which somewhere along the way, the getchar() code converts to EOF (where EOF is actually -1).
Depending on what the read() system call is reading, there are different even lower level rules for indicating end-of-file. In your example, you are reading from the Terminal, and the tty driver (NOT the Terminal, but the tty driver) has the convention that if it is in the correct mode, it will tell the read() system call end-of-file when the tty driver sees a specific character.
The normal Unix convention is that Control-D is used to indicate end-of-file. However, this can be changed (but normally it is not), or the tty driver can be put into "RAW" mode which will pass every character entered directly to the reading program and do nothing.
You can see the current terminal driver settings using the
stty all
command. Here are the settings for my Terminal session:
stty all
speed 9600 baud; 13 rows; 145 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
discard dsusp eof eol eol2 erase intr kill lnext
^O ^Y ^D <undef> <undef> ^? ^C ^U ^V
min quit reprint start status stop susp time werase
1 ^ ^R ^Q ^T ^S ^Z 0 ^W
Notice on the 4th from the bottom row, it says 'eof' and below that it says ^D (Control-D). This basically says that the tty driver will tell the read() system call end-of-file when the tty driver sees Control-D.
Control-Z is used for job control, which suspends your current process and allows you to start a different job, optionally putting the current job in the background (Control-Z, jobs, bg and fg commands). Control-Z has nothing to do with end-of-file on a Unix based tty interface.
However, I know that back in the good old days of DOS, a real Control-Z character in a text file WAS the end-of-file indicator. This was bad design even back then. Thankfully, Unix predated DOS and never used that ugly convention.
As to your program, it works for me:
#include<stdio.h>
main()
{
int c;
while((c=getchar())!=EOF)
{
putchar(c);
}
}
I compiled and ran it:
command prompt> gcc example.c -o example
command prompt> example
the quick brown fox jumps over the lazy dog # I typed this
the quick brown fox jumps over the lazy dog # putchar() text
^D # I entered Control-D
command prompt> # the program ends, and I get the command prompt
If you are not seeing this behavior, then I would look closely at your
stty settings.