About the EOF

K

Kies Lee

Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
tell me how can I type the EOF on the screen using the keyboard?
 
F

Flash Gordon

Kies said:
Hi everyone! I am a beginner in C. I knew the EOF is -1,

I'm sorry, but you misunderstand. EOF is a macro that expands to a
negative integer, and on many systems that integers is -1, but that is
not guaranteed. Whatever value EOF expands to is the value that various
IO functions return to indicate and end-of-file or error condition.
> but can anyone
tell me how can I type the EOF on the screen using the keyboard?

It depends on your system and is thus not topical here. It could be
control+Z, control+D or anything else.

You should have read the comp.lang.c FAQ before posting, in which case
you would have found http://c-faq.com/stdio/eofval.html
 
M

Malcolm

Kies Lee said:
Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
tell me how can I type the EOF on the screen using the keyboard?
It depends on your sytem.
Try CTRL-Z to generate an EOF from the keyboard in DOS.

You cannot print an EOF chararacter on the screen (normally). If you try,
what will probably happen is that it interprets a -1 as 255, which may be a
printable character or may not be.
To do this

printf("%***%c***\n", EOF);

it's probably technically undefined behaviour, but you will see the result.
 
V

Vladimir S. Oka

Malcolm said:
It depends on your sytem.
Try CTRL-Z to generate an EOF from the keyboard in DOS.

And CTRL-D on Linux/Unix... YMMV
You cannot print an EOF chararacter on the screen (normally).

True, but you can print its value, as it's guaranteed to be an
int. Store your return value into an int variable, and once you
get an EOF print it out as an integer.
If you try, what will probably happen is that it interprets a
-1 as 255, which may be a printable character or may not be.
To do this

printf("%***%c***\n", EOF);

it's probably technically undefined behaviour, but you will
see the result.

It's wrong in the sense that you're trying to print an int using
a char format specifier. If you used %d, you'd get the value of
EOF as defined on your system (it's not necessarily a valid
character).

Cheers

Vladimir
 
E

Emmanuel Delahaye

Kies Lee a écrit :
Hi everyone! I am a beginner in C. I knew the EOF is -1,

It's not. The standard says that EOF is a negative int. That said, it is
possible that your implementation choose the value of -1, as recommended
by POSIX (which is not part of the C-language, but a strong standard
either).
but can anyone
tell me how can I type the EOF on the screen using the keyboard?

Badly worded, but you probably want to know how to trig an end of
reading when reading from the keyboard. It's a system issue:

MS-DOS/Windows : crtl-z<enter>
Unixoid : ctrl-d

etc.
 
M

Martin Ambuhl

Kies said:
Hi everyone! I am a beginner in C. I knew the EOF is -1,

No, you know no such thing. EOF is a macro which expands to an integer
constant expression, with type int and a negative value. There is no
reason to suppose that that negative value is -1.
but can anyone
tell me how can I type the EOF on the screen using the keyboard?

You need to check the FAQ before posting questions. The question you
need to check in particular is found at
<http://c-faq.com/stdio/eofval.html>, "I have a simple little program
that reads characters until EOF, but how do I actually enter that
``EOF'' value from the keyboard? I see that EOF is defined by <stdio.h>
to be -1; am I supposed to enter -1?". Notice the similarity to your
question?
 
K

Keith Thompson

Kies Lee said:
Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
tell me how can I type the EOF on the screen using the keyboard?

No EOF isn't necessarily -1; the standard merely says that it's a
negative value. (Most, possibly all, implementations use -1, but you
shouldn't depend on it.)

Keep in mind that end-of-file is a condition, not a character; the
value EOF is used to indicate that you've reached the end of the file.

How you cause and end-of-file condition varies from one system to
another. On Unix-like systems control-D is most common (once at the
beginning of a line, twice in the middle of a line). On DOS and
Windows systems, control-Z is most common.
 

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
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top