Value of EOF

M

Matt

Alright, this is puzzling me. Here's what it says:

Exercise 1-7. Write a program to print the value of EOF.

Which is from the book "The C Programming Language". How would I go
about writing that program, I honestly don't understand what it means,
please help. Thanks.
 
J

Jason

Matt said:
Alright, this is puzzling me. Here's what it says:

Exercise 1-7. Write a program to print the value of EOF.

Which is from the book "The C Programming Language". How would I go
about writing that program, I honestly don't understand what it means,
please help. Thanks.

EOF is simply an integer.
 
M

Matt

0 or 1? If so, I just don't get how to do that...would it be like
this?:

#include <stdio.h>

main()
{
int val1, val2;

val1 = 1;
val2 = 0;

printf("EOF is equal to either: %d %d\n", val2, val1);
}

? I'm not sure...
 
R

Randy Howard

0 or 1? If so, I just don't get how to do that...would it be like
this?:

#include <stdio.h>

main()
{
int val1, val2;

val1 = 1;
val2 = 0;

printf("EOF is equal to either: %d %d\n", val2, val1);
}

? I'm not sure...

You've got to be kidding. Please say you're joking.
 
M

Martin Ambuhl

Matt said:

No, EOF must be some negative integer. Neither 0 nor 1 qualifies.
If so, I just don't get how to do that...would it be like
this?:

#include <stdio.h>
main()
{
int val1, val2;
val1 = 1;
val2 = 0;
printf("EOF is equal to either: %d %d\n", val2, val1);
}

Not even close. This really isn't so hard:

#include <stdio.h>

int main(void)
{
printf("EOF == %d\n", EOF);
return 0;
}

[(implementation-specific) output]
EOF == -1
 
J

Jason

Matt said:

Neither. EOF is an integer value that is guaranteed to NOT be a valid
char value. On my system it shows as -1, but it could easily be
anything else on your system.
#include <stdio.h>

main()
{
int val1, val2;

val1 = 1;
val2 = 0;

printf("EOF is equal to either: %d %d\n", val2, val1);
}

? I'm not sure...

How about printf("EOF=%d\n",EOF) ?
 
K

Keith Thompson

Matt said:
0 or 1? If so, I just don't get how to do that...would it be like
this?:

Since you snipped the context, it's hard to tell what your "0 or 1?"
refers to. Since I just read the parent article, I happen to know
that Jason wrote "EOF is simply an integer." is the parent article.
And the answer to your question is no; EOF does not have the value 0
or 1. (Why would you assume that it does?)

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Hint: EOF is a macro defined in <stdio.h>. Your program (not quoted
here) does not refer to EOF, so it can't print its value.
 
M

Matt

I'm sorry for being stupid, but I just didn't understand the question
in the book. Now I do, thanks for helping Jason and Martin Ambuhl
 
R

Randy Howard

Be kind now. We were all in that some boat before.

If you say so. In what schools do they teach that the set of
integers consists of the numbers 0 and 1 only? I want to be
sure not to send my children to one of them.
 
M

Matt

I never said those were the only integers I know. Shall I write them
all out for you?

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,etC.
I just didn't understand the question.
 
K

Keith Thompson

Matt said:
I never said those were the only integers I know. Shall I write them
all out for you?

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,etC.
I just didn't understand the question.

Several articles upthread, Jason wrote:

] EOF is simply an integer.

Your response was:

] 0 or 1? If so, I just don't get how to do that...would it be like
] this?:
]
] #include <stdio.h>
]
] main()
] {
] int val1, val2;
]
] val1 = 1;
] val2 = 0;
]
] printf("EOF is equal to either: %d %d\n", val2, val1);
] }
]
] ? I'm not sure...

I'm sure you understand that 0 and 1 aren't the only integers, but
what you wrote *seemed* to imply exactly that.

I'm not trying to give you a hard time, just explaining why some of us
reacted the way we did. (I still don't understand where your "0 or 1?"
comment came from, but that's not important; if you've got a good
handle on it now, I suggest we just drop it and move on.)
 
L

Lawrence Kirby

Neither. EOF is an integer value that is guaranteed to NOT be a valid
char value.

No, EOF is commonly defined as -1 on implementations where char is signed
i.e. can represent the value -1. EOF is required to be a negative value
which means that it can't be a valid *unsigned char* value. That's
relevant because functions like getchar() return a character represented
as an unsigned char value converted to int, so when such functions return
EOF it is a distinct value from any valid character (except in a certain
theoretical situation which isn't usually worth worrying about).
On my system it shows as -1, but it could easily be
anything else on your system.

As long as it is a negative int value.

Lawrence
 
C

Chris Croughton

No, EOF is commonly defined as -1 on implementations where char is signed
i.e. can represent the value -1. EOF is required to be a negative value
which means that it can't be a valid *unsigned char* value. That's
relevant because functions like getchar() return a character represented
as an unsigned char value converted to int, so when such functions return
EOF it is a distinct value from any valid character (except in a certain
theoretical situation which isn't usually worth worrying about).

You're referring to sizeof int == 1 or some other case?

Is there any chance of dropping the "plain char may be signed" from the
spec. totally? Since it may be signed or unsigned no conforming code
can depend on it being signed, and everywhere I've seen signed char is
more of a problem (indeed, I don't think I've ever seen it required).

Chris C
 
J

Jason

Lawrence said:
No, EOF is commonly defined as -1 on implementations where char is signed
i.e. can represent the value -1. EOF is required to be a negative value
which means that it can't be a valid *unsigned char* value.

Correct. I should have said EOF is guaranteed to not be a valid
unsigned char value.
 
K

Keith Thompson

Chris Croughton said:
Is there any chance of dropping the "plain char may be signed" from the
spec. totally? Since it may be signed or unsigned no conforming code
can depend on it being signed, and everywhere I've seen signed char is
more of a problem (indeed, I don't think I've ever seen it required).

Interesting idea. I doubt that it will happen because there are so
many implementations in which plain char is signed (in fact most of
the ones I'm familiar with), and there's probably non-portable code
that depends on that. The code will break anyway when ported to a
system with unsigned plain char. As for the implementations, since
they have to support unsigned char and signed char anyway, making
plain char equivalent to one rather than the other shouldn't be too
much of a burden.

I'm for it, but I don't think they'll listen to me.
 
C

CBFalconer

Qi said:
EOF equals to -1

WRONG. And please do not top-post, which is rude and unacceptable
in this newsgroup. I fixed this one.

EOF is a macro defined in stdio.h (and other places). All that is
known about it is that is negative, fits into an int, and is
outside the range of char. So the OPs original problem is
isometric to printing the value of an int.
 

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
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top