By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

main() does not return when getchar() reaches EOF

P: n/a
I essentially copied this program from K&R:

#include <stdio.h>
#include <stdlib.h>

int main()
{

int c;

while ((c=getchar())!=EOF)
putchar(c);

return 0;
}

When I run it it takes my input and prints it on the console but
doesn't exit. The program continues running. No matter what I input
it always continues running.

This is Dev-C++, uses gcc. Is it a compiler problem?
Jul 1 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
* Seeker:
I essentially copied this program from K&R:

#include <stdio.h>
#include <stdlib.h>

int main()
{

int c;

while ((c=getchar())!=EOF)
putchar(c);

return 0;
}

When I run it it takes my input and prints it on the console but
doesn't exit. The program continues running. No matter what I input
it always continues running.

This is Dev-C++, uses gcc. Is it a compiler problem?


No, it is a user problem. Specifically, you never generate an end of
file. In Unix, use Ctrl D to submit zero bytes, which is then
interpreted as end of file; in Windows, use Ctrl Z followed by return,
where the control character itself is interpreted as end of file.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 1 '06 #2

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in
news:4g*************@individual.net:
No, it is a user problem. Specifically, you never generate an
end of file. In Unix, use Ctrl D to submit zero bytes, which is
then interpreted as end of file; in Windows, use Ctrl Z followed
by return, where the control character itself is interpreted as
end of file.


I see. Thanks.
Jul 1 '06 #3

P: n/a
"Alf P. Steinbach" <al***@start.no> writes:
* Seeker:
I essentially copied this program from K&R:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
while ((c=getchar())!=EOF)
putchar(c);

return 0;
}
When I run it it takes my input and prints it on the console but
doesn't exit. The program continues running. No matter what I input
it always continues running.
This is Dev-C++, uses gcc. Is it a compiler problem?


No, it is a user problem. Specifically, you never generate an end of
file. In Unix, use Ctrl D to submit zero bytes, which is then
interpreted as end of file; in Windows, use Ctrl Z followed by return,
where the control character itself is interpreted as end of file.


<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 1 '06 #4

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
....
><OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
Actually, that is exactly what it does.
></OT>
Jul 1 '06 #5

P: n/a
In article <e8**********@news.xmission.comga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
...
<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.

Actually, that is exactly what it does.
That is actually exactly not what it does. ^D under Unix/Linux generates
an out of band EOF signal when used as the first character on a line on
console input. A double ^D does the same when not the first character.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 1 '06 #6

P: n/a
>"Alf P. Steinbach" <al***@start.nowrites:
>>... you [need to] generate an end of file [from keyboard input].
In Unix, use Ctrl D to submit zero bytes, which is then
interpreted as end of file; in Windows, use Ctrl Z followed by return,
where the control character itself is interpreted as end of file.
In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.orgwrote:
><OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
</OT>
This is indeed off topic, but I will take a dollar out of the
metaphorical jar. :-)

On a Unix-like system, keyboard input is routed through an
interpretation layer, often called a "tty driver" for historical
reasons. This interpretation layer has several modes, usually
called "cooked", "cbreak", and "raw". In "raw" mode all input
characters are passed through immediately and unchanged. In
"cbreak" mode, most characters are passed through immediately and
unchanged; a few are interpreted specially. In the most usual,
"cooked" mode, however, characters are collected up for input
editing, allowing the user to erase incorrect input, up to a point.

In cooked mode, there are a number of user-specified special
characters, including one called the "EOF character". This is
normally set to control-D. Internally, however, it is not really
an "EOF character" at all. Instead, it has almost the same
effect as pressing the ENTER or RETURN key.

The key labeled ENTER (or RETURN) usually sends a control-M to
the connected serial port. What? Oh, right, we have another
historical artifact here.

Modern keyboards send key-up/key-down codes over some sort of
connection (often, now, a USB or wireless channel). However, on
a typical Unix-like system, a software layer translates these
into what look like serial-port signals coming from an old-style
terminal device. These old-style terminal devices, common back
in the 1980s, would send control-M when you pushed ENTER.

Anyway, back to the "tty driver" software layer: when it sees a
control-M (in cooked mode), it translates it to a control-J (or
"line feed" or '\n') character. Then it falls through to the code
that handles a control-J. This code puts the control-J into the
edit buffer, and then sends the current contents of the edit buffer
up through the rest of the system's software layers, to be returned
from a read() system call and delivered to -- in this case -- your
program using C's "stdin" stream.

Most other ordinary characters simply go into the edit buffer, and
are (optionally) echoed back to the output serial port -- which,
in many cases today, is another fake, made out of yet another
software layer that connects to a display of some sort, or reroutes
to a window system, or some such; but that is yet another can of
worms. There they sit, waiting to be edited (deleted or added-to
or whatever) until you press ENTER.

If you press the EOF key (normally control-D), however, the tty
driver does almost the same thing that it does for control-J. For
control-J, it adds the '\n' to the edit buffer and sends the
contents back up the layers. For control-D, it skips the "add"
part, and sents the buffer contents up. IF THE BUFFER IS EMPTY,
this causes the read() system call to return 0. If the buffer
is *not* empty, the read() system call returns some positive
number, and C's "stdin" stream collects up a sequence of characters
that does *not* end with a '\n'.

Whenever a read() system call returns 0, a Unix-like system's stdio
interprets that as "end of input", and delivers an EOF from the
stream. So pressing ^D does, in this case, "submit zero bytes",
as long as you press it with an empty cooked-mode edit buffer.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 1 '06 #7

P: n/a
In article <J1********@cwi.nl>, Dik T. Winter <Di********@cwi.nlwrote:
>In article <e8**********@news.xmission.com>
ga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
...
><OT>
>I'm not sure what you mean by "submit zero bytes", but I don't believe
>that's what it does.
Actually, that is exactly what it does.

That is actually exactly not what it does. ^D under Unix/Linux generates
an out of band EOF signal when used as the first character on a line on
console input. A double ^D does the same when not the first character.
Wrong. The phrase "submit zero bytes" (although worded a little
strangely, but we're used to this "pigeon English" around here) is quite
accurate.

This is all stunningly OT, of course, but all you have to do is check
the return value from read() (Yes, it is OT, we know that) to get the point.

It really, really, isn't any kind of "out of band" signal.

Jul 2 '06 #8

P: n/a
Kenny McCormack wrote:
In article <J1********@cwi.nl>, Dik T. Winter <Di********@cwi.nlwrote:
>In article <e8**********@news.xmission.com>
ga*****@xmission.xmission.com (Kenny McCormack) writes:
>>In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
...
<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
Actually, that is exactly what it does.
That is actually exactly not what it does. ^D under Unix/Linux generates
an out of band EOF signal when used as the first character on a line on
console input. A double ^D does the same when not the first character.

Wrong. The phrase "submit zero bytes" (although worded a little
strangely, but we're used to this "pigeon English" around here) is quite
accurate.

This is all stunningly OT, of course, but all you have to do is check
the return value from read() (Yes, it is OT, we know that) to get the point.

It really, really, isn't any kind of "out of band" signal.
As Chris' post excellently explained, you're both right. Or wrong, as the
case may be. ^D really is out-of-band for some meanings of "out-of-band"
(since the upper layers never see it) and in some way it really does cause
zero bytes to be transmitted.

Drinks for everyone.

S.
Jul 2 '06 #9

P: n/a
Chris Torek wrote:
>"Alf P. Steinbach" <al***@start.nowrites:
>>... you [need to] generate an end of file [from keyboard input].
In Unix, use Ctrl D to submit zero bytes, which is then
interpreted as end of file; in Windows, use Ctrl Z followed by return,
where the control character itself is interpreted as end of file.

In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.orgwrote:
><OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
</OT>

This is indeed off topic, but I will take a dollar out of the
metaphorical jar. :-)

On a Unix-like system, keyboard input is routed through an
interpretation layer, often called a "tty driver" for historical
reasons. This interpretation layer has several modes, usually
called "cooked", "cbreak", and "raw". In "raw" mode all input
characters are passed through immediately and unchanged. In
"cbreak" mode, most characters are passed through immediately and
unchanged; a few are interpreted specially. In the most usual,
"cooked" mode, however, characters are collected up for input
editing, allowing the user to erase incorrect input, up to a point.

In cooked mode, there are a number of user-specified special
characters, including one called the "EOF character". This is
normally set to control-D. Internally, however, it is not really
an "EOF character" at all. Instead, it has almost the same
effect as pressing the ENTER or RETURN key.

The key labeled ENTER (or RETURN) usually sends a control-M to
the connected serial port. What? Oh, right, we have another
historical artifact here.

Modern keyboards send key-up/key-down codes over some sort of
connection (often, now, a USB or wireless channel). However, on
a typical Unix-like system, a software layer translates these
into what look like serial-port signals coming from an old-style
terminal device. These old-style terminal devices, common back
in the 1980s, would send control-M when you pushed ENTER.

Anyway, back to the "tty driver" software layer: when it sees a
control-M (in cooked mode), it translates it to a control-J (or
"line feed" or '\n') character. Then it falls through to the code
that handles a control-J. This code puts the control-J into the
edit buffer, and then sends the current contents of the edit buffer
up through the rest of the system's software layers, to be returned
from a read() system call and delivered to -- in this case -- your
program using C's "stdin" stream.

Most other ordinary characters simply go into the edit buffer, and
are (optionally) echoed back to the output serial port -- which,
in many cases today, is another fake, made out of yet another
software layer that connects to a display of some sort, or reroutes
to a window system, or some such; but that is yet another can of
worms. There they sit, waiting to be edited (deleted or added-to
or whatever) until you press ENTER.

If you press the EOF key (normally control-D), however, the tty
driver does almost the same thing that it does for control-J. For
control-J, it adds the '\n' to the edit buffer and sends the
contents back up the layers. For control-D, it skips the "add"
part, and sents the buffer contents up. IF THE BUFFER IS EMPTY,
this causes the read() system call to return 0. If the buffer
is *not* empty, the read() system call returns some positive
number, and C's "stdin" stream collects up a sequence of characters
that does *not* end with a '\n'.

Whenever a read() system call returns 0, a Unix-like system's stdio
interprets that as "end of input", and delivers an EOF from the
stream. So pressing ^D does, in this case, "submit zero bytes",
as long as you press it with an empty cooked-mode edit buffer.
Another fine example of why we should all read whatever Chris Torek
writes. Thank you Chris.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 2 '06 #10

P: n/a
Keith Thompson wrote:
"Alf P. Steinbach" <al***@start.nowrites:
>No, it is a user problem. Specifically, you never generate an end of
file. In Unix, use Ctrl D to submit zero bytes, which is then
interpreted as end of file; in Windows, use Ctrl Z followed by return,
where the control character itself is interpreted as end of file.

<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
</OT>
This is how I understand it: If you type a line of text and terminate it
with Ctrl-D instead of a carriage return, it submits what you have typed
so far, without a newline character. Thus Ctrl-D can be seen as a
command to submit what has been typed so far. If you press Ctrl-D at the
beginning of a line, when there is nothing to submit, that can be seen
as submitting zero bytes. The terminal driver's response to submission
of zero bytes is to indicate end of file.

--
Simon.
Jul 3 '06 #11

P: n/a
Kenny McCormack wrote:
In article <J1********@cwi.nl>, Dik T. Winter <Di********@cwi.nlwrote:
>In article <e8**********@news.xmission.com>
ga*****@xmission.xmission.com (Kenny McCormack) writes:
>>In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
...
<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
Actually, that is exactly what it does.
That is actually exactly not what it does. ^D under Unix/Linux generates
an out of band EOF signal when used as the first character on a line on
console input. A double ^D does the same when not the first character.

Wrong. The phrase "submit zero bytes" (although worded a little
strangely, but we're used to this "pigeon English" around here) is quite
accurate.
No. Typically the unix (which is what we are discussing) tty driver
captures various keystrokes and translate them to signals/condition/
internal tty layer manipulation. ctrl+d is usually just hooked up
to generate EOF to the reading end of the tty - pseudo tty mostly these
days..
Jul 3 '06 #12

P: n/a
In article <44**********@news.peopletelecom.com.au>,
Simon Biber <ne**@ralmin.ccwrote:
....
>This is how I understand it: If you type a line of text and terminate it
with Ctrl-D instead of a carriage return, it submits what you have typed
so far, without a newline character. Thus Ctrl-D can be seen as a
command to submit what has been typed so far. If you press Ctrl-D at the
beginning of a line, when there is nothing to submit, that can be seen
as submitting zero bytes.
So far, so good. (Of course, this is all Unix (-like) specific, so all
the usual blah, blah, blah applies as far as topicality)
>The terminal driver's response to submission
of zero bytes is to indicate end of file.
But here is where you've driven off the road. The "terminal driver"
does not "indicate" end of file. All it does is return a count of 0
(which is where you were headed up above) and the program (or, to be
more precise, the layer above the read()) call sees that 0 and
interprets it (at its own risk) as an indication of "end of file".

Jul 3 '06 #13

P: n/a
In article <44******@news.wineasy.se>,
Nils O. Selåsdal <no******@asgaard.homelinux.orgwrote:
....
>No. Typically the unix (which is what we are discussing) tty driver
captures various keystrokes and translate them to signals/condition/
internal tty layer manipulation. ctrl+d is usually just hooked up
to generate EOF to the reading end of the tty - pseudo tty mostly these
days..
Wrong. Be sure to pick up your lovely parting gifts on your way out.

Jul 3 '06 #14

P: n/a
* Nils O. Selåsdal:
Kenny McCormack wrote:
>In article <J1********@cwi.nl>, Dik T. Winter <Di********@cwi.nlwrote:
>>In article <e8**********@news.xmission.com>
ga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
...
<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
Actually, that is exactly what it does.
That is actually exactly not what it does. ^D under Unix/Linux
generates
an out of band EOF signal when used as the first character on a line on
console input. A double ^D does the same when not the first character.

Wrong. The phrase "submit zero bytes" (although worded a little
strangely, but we're used to this "pigeon English" around here) is quite
accurate.
No. Typically the unix (which is what we are discussing) tty driver
captures various keystrokes and translate them to signals/condition/
internal tty layer manipulation. ctrl+d is usually just hooked up
to generate EOF to the reading end of the tty - pseudo tty mostly these
days..
Although off-topic, since it was my (accurate) comment that started
this: you have just argued that you cannot affect the speed of a car
using the gas pedal, because for one particular car you know about it's
the motor (and brakes) that directly effects changes of speed. Well, to
be really accurate you should get down to the level of interactions
between the wheels and the ground, that's where the real (tr)action is.
So, what about /voltages/ when you hit Ctrl D -- how relevant is
that, and how general are comments about detailed voltages, do you think?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 3 '06 #15

P: n/a
Simon Biber <ne**@ralmin.ccwrites:
Keith Thompson wrote:
>"Alf P. Steinbach" <al***@start.nowrites:
>>No, it is a user problem. Specifically, you never generate an end of
file. In Unix, use Ctrl D to submit zero bytes, which is then
interpreted as end of file; in Windows, use Ctrl Z followed by return,
where the control character itself is interpreted as end of file.
<OT>
I'm not sure what you mean by "submit zero bytes", but I don't believe
that's what it does.
</OT>

This is how I understand it: If you type a line of text and terminate
it with Ctrl-D instead of a carriage return, it submits what you have
typed so far, without a newline character. Thus Ctrl-D can be seen as
a command to submit what has been typed so far. If you press Ctrl-D at
the beginning of a line, when there is nothing to submit, that can be
seen as submitting zero bytes. The terminal driver's response to
submission of zero bytes is to indicate end of file.
Ok. I interpreted "submit zero bytes" to mean "submit bytes whose
value is 0". It's clear that that's not what was meant -- but if that
*were* what control-D did, then the phrase "submit zero byte" would
have been a reasonable way to describe it.

(Since we all know what was meant, and the details are off-topic
anyway, I suggest there's no further need to debate this.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 3 '06 #16

P: n/a
Kenny McCormack wrote:
>
In article <44**********@news.peopletelecom.com.au>,
Simon Biber <ne**@ralmin.ccwrote:
[...]
The terminal driver's response to submission
of zero bytes is to indicate end of file.

But here is where you've driven off the road. The "terminal driver"
does not "indicate" end of file. All it does is return a count of 0
(which is where you were headed up above) and the program (or, to be
more precise, the layer above the read()) call sees that 0 and
interprets it (at its own risk) as an indication of "end of file".
According to the man pages here, the EOF character does the following
from a "terminal":

EOF Special character on input and is recognized if the ICANON flag
is set. When received, all the bytes waiting to be read are
immediately passed to the process, without waiting for a newline,
and the EOF is discarded. Thus, if there are no bytes waiting
(that is, the EOF occurred at the beginning of a line), a byte
count of zero is returned from the read(), representing an end-
of-file indication. If ICANON is set, the EOF character is dis-
carded when processed.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jul 3 '06 #17

P: n/a
In article <44***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.netwrote:
>Kenny McCormack wrote:
>>
In article <44**********@news.peopletelecom.com.au>,
Simon Biber <ne**@ralmin.ccwrote:
[...]
>The terminal driver's response to submission
of zero bytes is to indicate end of file.

But here is where you've driven off the road. The "terminal driver"
does not "indicate" end of file. All it does is return a count of 0
(which is where you were headed up above) and the program (or, to be
more precise, the layer above the read()) call sees that 0 and
interprets it (at its own risk) as an indication of "end of file".

According to the man pages here, the EOF character does the following
from a "terminal":

EOF Special character on input and is recognized if the ICANON flag
is set. When received, all the bytes waiting to be read are
immediately passed to the process, without waiting for a newline,
and the EOF is discarded. Thus, if there are no bytes waiting
(that is, the EOF occurred at the beginning of a line), a byte
count of zero is returned from the read(), representing an end-
of-file indication. If ICANON is set, the EOF character is dis-
carded when processed.
Thank you for making my case. Well done.

Jul 3 '06 #18

P: n/a
Kenny McCormack wrote:
In article <44**********@news.peopletelecom.com.au>,
Simon Biber <ne**@ralmin.ccwrote:
...
>This is how I understand it: If you type a line of text and terminate it
with Ctrl-D instead of a carriage return, it submits what you have typed
so far, without a newline character. Thus Ctrl-D can be seen as a
command to submit what has been typed so far. If you press Ctrl-D at the
beginning of a line, when there is nothing to submit, that can be seen
as submitting zero bytes.

So far, so good. (Of course, this is all Unix (-like) specific, so all
the usual blah, blah, blah applies as far as topicality)
>The terminal driver's response to submission
of zero bytes is to indicate end of file.

But here is where you've driven off the road. The "terminal driver"
does not "indicate" end of file. All it does is return a count of 0
(which is where you were headed up above) and the program (or, to be
more precise, the layer above the read()) call sees that 0 and
interprets it (at its own risk) as an indication of "end of file".
Yes, I was misusing the term "terminal driver". By that term I meant to
include everything in between the user's keyboard and the eventual EOF
return from getchar. At some point there is an EOF generated, and that
may be by getchar / getc / fgetc themselves.

--
Simon.

--
Posted via a free Usenet account from http://www.teranews.com

Jul 12 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.