info about EOF

A

alok

if(getchar() != EOF)

what is the value of EOF and for what keyborad input the if statement
will fail?
 
S

Seebs

if(getchar() != EOF)

what is the value of EOF and for what keyborad input the if statement
will fail?

This question seems awfully homeworky.

So, help us out here. What have you already tried to do in order to
figure out the answer to this question? What are you stuck on?

-s
 
M

Morris Keesan

if(getchar() != EOF)

what is the value of EOF and for what keyborad input the if statement
will fail?

The value of EOF is EOF, which is a negative value of type (int), which
cannot be represented in an (unsigned char). The exact value is up to
the implementation, and you should try not to know what it is, because
if you know the value you're liable to write code which expects that value,
and that code will fail when you try to run it on a different platform.

What keyboard input generates an EOF is system-dependent, and on the
systems I most often write C code for, the EOF character can be changed
by the user.
 
M

Malcolm McLean

What's an "EOF character" ?
In some systems, notably MS-DOS, end of file was indicated by a
special character, control-Z.

Since in C control-Z has to be treated as a valid char, fgetc() and
related functions had to intercept control-Z and translate to an EOF.
EOF is always negative.
 
K

Kenny McCormack

The value of EOF is EOF, which is a negative value of type (int), which
cannot be represented in an (unsigned char). The exact value is up to
the implementation, and you should try not to know what it is, because
if you know the value you're liable to write code which expects that value,
and that code will fail when you try to run it on a different platform.

Be wary anytime anyone tells you that you shouldn't know something. This is
very much the stance of the medieval Church.

If you want to know what EOF is on your system, try this:

$ printf '#include <stdio.h>\nmain() {printf("EOF = %%d\\n",EOF);return 0;}'|gcc -xc - && ./a.out && rm ./a.out

which displays EOF = -1 on my system.
 
M

Mark Bluemel

What's an "EOF character" ?
I took it as being shorthand for
"the keystroke (combination) which when entered on a keyboard
(or similar input device) indicates to the operating system
that end of file has been reached. This will be represented
by the C support libraries by delivering the EOF value as the
result of a read from this device."
 
M

Mark Bluemel

Be wary anytime anyone tells you that you shouldn't know something. This is
very much the stance of the medieval Church.

That isn't what Morris said and he explained the risks associated with
assuming a value for EOF.
 
M

Mark Bluemel

if(getchar() != EOF)

what is the value of EOF

The language specification says
" EOF
.... expands to an integer constant expression, with type int and a
negative value, that is returned by several functions to indicate
end-of-file, that is, no more input from a stream"

Note that the precise value is not specified - it is frequently -1
but this is not required.
and for what keyborad input the if statement
will fail?

This is not a question about the language, but about the platform.
Different platforms have different ways of representing end-of-input
on a keyboard. Common examples are ctrl-z (windows) and ctrl-d (default
for at least some Unix/Linux shells). These values can be changed in
some environments (for example with the stty command on Unix/Linux).

Try reading Section 12 of the C FAQ at http://c-faq.com - you've more
or less asked questions 12.1 and 12.1b
 
K

Kenny McCormack

That isn't what Morris said ...

I think even the most experienced CLC word parser would be very hard-pressed
to explain how Morris didn't say that you shouldn't know the value of EOF.
 
M

Malcolm McLean

Be wary anytime anyone tells you that you shouldn't know something.  This is
very much the stance of the medieval Church.
Information hiding is a good ecclesiastical as well as programming
principle.

If you give people information they don't need they might misuse it,
bringing down the entire system.
 
M

Morris Keesan

Be wary anytime anyone tells you that you shouldn't know something.
This is
very much the stance of the medieval Church.

If you want to know what EOF is on your system, try this:

$ printf '#include <stdio.h>\nmain() {printf("EOF = %%d\\n",EOF);return
0;}'|gcc -xc - && ./a.out && rm ./a.out

which displays EOF = -1 on my system.

And what would you do with this information, in any way which isn't
likely to cause you problems?
 
T

Tom St Denis

I think even the most experienced CLC word parser would be very hard-pressed
to explain how Morris didn't say that you shouldn't know the value of EOF..

I don't see how knowing the exact value of EOF helps you in any way.

Tom
 
M

Morris Keesan

I took it as being shorthand for
"the keystroke (combination) which when entered on a keyboard
(or similar input device) indicates to the operating system
that end of file has been reached. This will be represented
by the C support libraries by delivering the EOF value as the
result of a read from this device."

I intended it as shorthand for
"the value stored in c_cc[VEOF] in an object of type (struct termios)",
for which the description at
<http://pubs.opengroup.org/onlinepubs/009695399/basedefs/termios.h.html>
is "EOF character."
 
M

Malcolm McLean

I don't see how knowing the exact value of EOF helps you in any way.
Say you see

while(ch = fgetc(fp) != -1)
do_something(ch);

The idiot programmer should have used EOF. But that's not the problem.
You won't waste time changing -1 to EOF, recompiling and retesting,
which might take a long time. You'll look elsewhere and hopefully spot
what is actually wrong.
 
K

Kenny McCormack

Information hiding is a good ecclesiastical as well as programming
principle.

If you give people information they don't need they might misuse it,
bringing down the entire system.

Spoken like a good Catholic.

The Pope would be proud of you.

--
Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
an 8 bit operating system based on a 4 bit architecture from a 2 bit company
that can't stand 1 bit of competition.

Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
operating system based on a 4 bit architecture from a 2 bit company that
can't stand 1 bit of competition.
 
O

osmium

Tom said:
I don't see how knowing the exact value of EOF helps you in any way.

But more fundamental, how does it *hurt* to know what it is?

In a similar vein, I doubt if it helps to know pi to more than 20 places.
But many people know how to find or even compute this value. Some people
just want to know for the sake of knowing - I am one of those people. Of
course they should be warned about the hazards of actually using the value.

I liked the quote about the church; unfortunately I think there have been
threads where the quote would have been a better fit rather than this
particular thread.
 
T

Tom St Denis

Tom said:
But more fundamental, how does it *hurt* to know what it is?

Fine, but I wouldn't allow my developers to use -1 in place of EOF.
In a similar vein, I doubt if it helps to know pi to more than 20 places.

Except, that pi by definition cannot change. EOF is defined as "a
negative integer." it could be -2 for all it matters...
But many people know how to find or even compute this value.  Some people
just want to know for the sake of knowing - I am one of those people. Of
course they should be warned about the hazards of actually using the value.

Then look it up, but don't presume that knowing the value of EOF helps
you develop your application.
I liked the quote about the church; unfortunately I think there have been
threads where the quote would have been a better fit rather than this
particular thread.

That's just Kenny trying to be grandios.

Tom
 
A

Anders Wegge Keller

Be wary anytime anyone tells you that you shouldn't know something.
This is very much the stance of the medieval Church.

Can you elaborate on that?

Personally, I don't see a problem in knowing the value of EOF. But
unless you're implementing your own fgetc() for a particular platform,
the knowledge is next to useless.

And actually comparing with -1 (or whatever the particular
implementation uses) instead of the symbol, is definitely a step down
the road to portability trouble.
 
O

osmium

Sherm Pendley said:
In general, I think it's poor practice to "look behind the curtain" of
whatever level of abstraction you happen to be working at. At any given
level, there are going to be implementation details that simply don't
matter to the task at hand, and concerning one's self with them only
serves as an unnecessary distraction.

"EOF is often -1" has five words in it. Case closed. As a side effect it
explains, it an elliptical way, why some functions return an int rather than
a char.

How many word are in the explanation about *why* it is poor practice?
 

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,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top