473,388 Members | 1,032 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,388 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 5249
"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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.