EOF question..

C

Chad

Given something like:

#include <stdio.h>

int main(void) {
int c;

while( (c=getchar()) != EOF )
putchar(c);
return 0;
}


When I run and twice hit ctrl-d (indicating EOF on my system), I get
the following

cdalten@linux:~> ./ef
la la cdalten@linux:~>

Why does 'la ' get echoed back after the first EOF?

Chad
 
R

Richard Tobin

Why does 'la ' get echoed back after the first EOF?

See the recent thread about EOF in this newsgroup. Short answer: your
first ctrl-D is not an end-of-file; it's a buffer flush
command. Ctrl-D is only sends end-of-file after a line break or
another ctrl-D. This is a feature of unix, not C itself.

-- Richard
 
C

CBFalconer

Chad said:
Given something like:

#include <stdio.h>
int main(void) {
int c;
while( (c=getchar()) != EOF )
putchar(c);
return 0;
}

When I run and twice hit ctrl-d (indicating EOF on my system),
I get the following

cdalten@linux:~> ./ef
la la cdalten@linux:~>

Why does 'la ' get echoed back after the first EOF?

Because the first "la" is not from your program, but from the input
system echoing the keys you typed. The second "la" is the programs
output. The input was buffered until either EOF or '\n' was
encountered, so your program saw nothing until that point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top