468,491 Members | 2,024 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Why getchar() doesn't quit if EOF isn't the first char

Thank you for your time.
#include <stdio.h>

int main(void)
{
int c;

while ((c = getchar()) != EOF){
putchar(c);
fflush(stdout);
}
return 0;
}
/* [a console interact session]

aaa^Zbbb [INPUT surrounding ^Z followed by enter]
[a single enter here]
aaa [output]

^Z [here, ^Z followed by an enter]

*/

Nov 14 '07 #1
8 5018
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
Subject: Why getchar() doesn't quit if EOF isn't the first char
The answer to this question is actually Unix-specific. The best
answer I have seen is in _The Unix Programming Environment_ by
Kernighan and Pike. I'd encourage you to obtain a copy, because
it is a good book. I see that amazon.com has used copies for
under $10.
--
Ben Pfaff
http://benpfaff.org
Nov 14 '07 #2
Ben Pfaff <bl*@cs.stanford.eduwrites:
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
>Subject: Why getchar() doesn't quit if EOF isn't the first char

The answer to this question is actually Unix-specific. The best
answer I have seen is in _The Unix Programming Environment_ by
Kernighan and Pike. I'd encourage you to obtain a copy, because
it is a good book. I see that amazon.com has used copies for
under $10.
Since his sample session shows ^Z, not ^D, it's probably not Unix.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 14 '07 #3
lovecreatesbea...@gmail.com wrote:
[ Subject: Why getchar() doesn't quit if EOF isn't the first char ]
#include <stdio.h>

int main(void)
{
int c;

while ((c = getchar()) != EOF){
putchar(c);
fflush(stdout);
}
return 0;
}
/* [a console interact session]

aaa^Zbbb [INPUT surrounding ^Z followed by enter]
[a single enter here]
aaa [output]

^Z [here, ^Z followed by an enter]

*/
EOF isn't a character. It's a value returned by getchar() to indicate an
end-of-file condition; that value is distinct from any character value.

Apparently when you enter control-Z in the middle of a line, it doesn't trigger
and end-of-file condition. The manner in which this condition will be triggered
depends on your operating system and your C implementation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 14 '07 #4
On Wed, 14 Nov 2007 06:01:22 -0800, "lovecreatesbea...@gmail.com"
<lo***************@gmail.comwrote:
>Thank you for your time.
#include <stdio.h>

int main(void)
{
int c;

while ((c = getchar()) != EOF){
putchar(c);
fflush(stdout);
}
return 0;
}
/* [a console interact session]

aaa^Zbbb [INPUT surrounding ^Z followed by enter]
[a single enter here]
aaa [output]

^Z [here, ^Z followed by an enter]

*/
Firstly, EOF is not a character at all. It is a special value
returned by some input functions to indicate the end-of-file condition
has been detected on the input stream.

Secondly, ^Z does not, by itself, cause this condition. It is a
convention used by some systems to allow a stream to simulate this
condition. But the convention may have restrictions. It is possible
your system has the restriction that ^Z will only serve this purpose
if it immediately follows an ENTER.

If you really want to know what is happening, you should print c as an
integer (I prefer hex) rather than a character. This way, you will
see exactly what characters getchar obtains. You might want to change
the while to a do-while so you see the EOF value also. (Currently you
may not be able to tell the difference between entering a ^Z and
entering a ^C.)
Remove del for email
Nov 15 '07 #5
In article <13*************@corp.supernews.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>>There is no such thing as "an EOF char".
>Of course there is in Operating Systems terms,
>EOF is a condition set on the stream by the OS when no more data is
available to read.
In some operating systems. In others, it appears as a character
marking the end of the file.
>I would assume that you're referring to the end-of-file marker that a
few operating systems use for legacy reasons in text files. This is not
actually an EOF character, and for reference,
How do you determine whether it's "actually" an EOF character? It's
just terminology, there's no fact of the matter.

It's pointless to argue "EOF doesn't mean so-and-so". The term is used
outside C, and it is natural to refer to non-C uses of it when talking
about EOF in C.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 20 '07 #6
Mark Bluemel wrote:
stty - change and print terminal line settings

[snip]
Special characters:
* dsusp CHAR
CHAR will send a terminal stop signal once input flushed

eof CHAR
CHAR will send an end of file (terminate the input)

*shrug*.

I gave a worked example showing that Crtl-D, 0x04 is NOT an EOF, and
will not terminate reading from a file.

I don't dispute you can press Ctrl-D and send an EOF signal to your
application, but encountering character 0x04 in a stream is not the same
as sending that stream a signal to say "end of data reached".

I have a feeling we had this dull discussion about 12 months ago. I was
right then too.... :-)
Nov 20 '07 #7
Richard Tobin wrote:
How do you determine whether it's "actually" an EOF character?
Because the ASCII character set has no character called EOF. It has an
EOT and ESC, which occupy the positions commonly associated with the
control sequences many OSen use to send an end-of-data signal from the
keyboard.
EBCDIC also has no EOF character.
>It's just terminology, there's no fact of the matter.
On that basis, nothing is fact.
It's pointless to argue "EOF doesn't mean so-and-so". The term is used
outside C, and it is natural to refer to non-C uses of it when talking
about EOF in C.
Agreed.
Nov 20 '07 #8
In article <13*************@corp.supernews.comMark McIntyre <ma**********@spamcop.netwrites:
Richard Tobin wrote:
How do you determine whether it's "actually" an EOF character?

Because the ASCII character set has no character called EOF. It has an
EOT and ESC, which occupy the positions commonly associated with the
control sequences many OSen use to send an end-of-data signal from the
keyboard.
EBCDIC also has no EOF character.
^D is indeed EOT (End Of Transmission), but ^Z is not ESC but SUB.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Luke Wu | last post: by
149 posts views Thread by Christopher Benson-Manica | last post: by
4 posts views Thread by outforblood74 | last post: by
19 posts views Thread by mailursubbu | last post: by
5 posts views Thread by Jonathan | last post: by
25 posts views Thread by ehabaziz2001 | last post: by
22 posts views Thread by arnuld | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.