How to input EOF?

D

Dean

I wrote a short c code as follow:

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

It works,but I don't know how to input the EOF so that I can stop the
program,hope you can help me,thanks.
 
V

Vallabha

I wrote a short c code as follow:

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

}

It works,but I don't know how to input the EOF so that I can stop the
program,hope you can help me,thanks.

Enter "^D" (Control-D).

cheers
-Vallabha
==============
S7 Software solutions
 
L

Lew Pitcher

Enter "^D" (Control-D).

Oh? Which OS, shell, and settings? If you say "Unix", then prepare to defend
your statement (and read up on stty).

For Microsoft Windows, the OP would have to enter ^Z on an empty line


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
C

CBFalconer

Lew said:
Oh? Which OS, shell, and settings? If you say "Unix", then
prepare to defend your statement (and read up on stty).

For Microsoft Windows, the OP would have to enter ^Z on an empty
line

As a curiosity, note that entering (without the quotes):

"aBS^Z"

where BS is the backspace, and ^Z is Control-Z, is an EOF on
Winders or MsDOS. Replace the BS with something else and it isn't.
 
R

Richard Tobin

It works,but I don't know how to input the EOF so that I can stop the
program,hope you can help me,thanks.
[/QUOTE]
Enter "^D" (Control-D).

As other have pointed out, the exact character may vary - control-Z is
also common.

But a more important point is that you're not typing in an "EOF character"
in the sense that C sees it. You're typing something that indicates the
end of file, and it is quite unrelated to the EOF value returned by
getc() etc. getc() returns EOF even at the end of a disk file, which
on most modern operating systems doesn't have any kind of character
representing it in the file: it's just what getc() returns when it
gets to the end of the file.

-- Richard
 
K

Keith Thompson

CBFalconer said:
As a curiosity, note that entering (without the quotes):

"aBS^Z"

where BS is the backspace, and ^Z is Control-Z, is an EOF on
Winders or MsDOS. Replace the BS with something else and it isn't.

Of course. Typing backspace changes the formerly empty line into an
empty line. The same thing happens on Unix-like systems (though the
particular characters used can be reconfigured).

The more general point is that the system can perform various
operations on the input you type before presenting it to your program.
(Some systems may let you reconfigure this behavior in various ways.)

What the program sees isn't necessarily what was typed at the keyboard
-- but that's not a problem as far as the standard is concerned,
because it doesn't even mention keyboards.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top