473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 5246
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
4
by: outforblood74 | last post by:
Ok here's the deal, the for each statment using VB 6 doesn't loop all the objects. I would like it to close all the IE browsers that are open, but it doesn't work. And now I'm concerned that its...
19
by: mailursubbu | last post by:
HI, Below is my program. I compiled it through g++. Now strange thing is, getc is not reading the data instead its printing the previously read data ! Please some one let me know whats wrong. ...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
22
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.