how to enter a C EOF character in terminal
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I already searched and went to this thread here which has been archieved...
http://discussions.apple.com/thread.jspa?messageID=5069197�
It says EOF can be entered using ctrl-d but that doesn't seem to end the input stream. my code is as follows...
#include<stdio.h>
main()
{
int c;
while((c=getchar())!=EOF)
{
putchar(c);
}
}
any help is appreciated.
PS: Referring to The C programming language, K&R
Message was edited by: AceNeerav
Message was edited by: AceNeerav
4 replies
Sign in or join Beachball Help to reply.
- 0
J D McIninch ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
🙂 In C, EOF is a value outside the range of permitted values for a character. It is explicitly not a character. Specifically, ASCII values are 1-byte, while EOF is a value that will not fit in 1-byte, but requires a minimum of 2 bytes. getchar() returns type 'int' rather than type 'char' specifically so that it can signal that the end of stream has been reached.
CONTROL-D does signal the terminal to close the standard input (stdin). However, keep in mind that , by default, the standard input is line-buffered. That means that no data is sent until the buffer is full (about 8K) or until the return key is pressed.
So, in short. Press ctrl-D to signal and end to stdin, and then the enter key to return the buffer.
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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 allcommand. 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 ^WNotice 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 promptIf you are not seeing this behavior, then I would look closely at your
stty settings. - 0
johncmurphy ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
EOF on the Mac requires a 3-step sequence:
ctrl-Q, ctrl-D, return - 0
AceNeerav ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
i am confused as how to use ctrl-z and ctrl-d