newbie EOF question

S

shan

Hi,
I am trying to do the exercises in K & R and in ex 1-7
the problem is to find out the value of EOF. I wrote the
program given below, but an error is thrown during
compilation. I am using GCC in cygwin environment.
The program is:

#include<stdio.h>
int main()
{
int val_eof;
while((val_eof=getchar())=EOF)
printf("The value of EOF is %d",val_eof);
return 0;
}


The error is

MurugesanSH@INP-Murugesans /cygdrive/d/shan-prog/cprog
$ gcc -o eof eof.c
eof.c: In function `main':
eof.c:5: error: invalid lvalue in assignment

Any help is appreciated
Brgds
Sha
 
M

Mike Wahler

Hi,
I am trying to do the exercises in K & R and in ex 1-7
the problem is to find out the value of EOF. I wrote the
program given below, but an error is thrown during
compilation. I am using GCC in cygwin environment.
The program is:

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

= is an 'operator'. You've used it twice in the above
statement. What does = do? :)

-Mike
 
C

CBFalconer

shan said:
.... snip ...

#include<stdio.h>
int main()
{
int val_eof;
while((val_eof=getchar())=EOF)
printf("The value of EOF is %d",val_eof);
return 0;
}

The error is

MurugesanSH@INP-Murugesans /cygdrive/d/shan-prog/cprog
$ gcc -o eof eof.c
eof.c: In function `main':
eof.c:5: error: invalid lvalue in assignment

To quote a certain Pop, engage brain. Notice the '5:' just before
the word 'error'. This might cause you to examine the 5th line of
your source. (that examination might also encourage you to
sprinkle the odd blank about for readability). The next clue
might be the word assignment, and this might encourage you to look
at phrases surrounding any assignment operators. Hint: concentrate
on line 5. How many of these operators do you see? You might
even think of searching your cbook for the meaning of lvalue.

The other alternative is to flop on your back with all four limbs
in the air, and wait for some nice passing person to scratch your
stomach. They might even feed you. Alternatively they may kick
you.
 
B

Barry Schwarz

Hi,
I am trying to do the exercises in K & R and in ex 1-7
the problem is to find out the value of EOF. I wrote the
program given below, but an error is thrown during
compilation. I am using GCC in cygwin environment.
The program is:

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

Others have pointed the error here.
printf("The value of EOF is %d",val_eof);

Wouldn't it be easier to simply print EOF directly. It's a macro so
its value can't change because it was returned from some function.

print("The value of EOF is %d",EOF);
return 0;
}



<<Remove the del for email>>
 
M

Mac

Others have pointed the error here.


Wouldn't it be easier to simply print EOF directly. It's a macro so
its value can't change because it was returned from some function.

print("The value of EOF is %d",EOF);

ITYM:
printf("The value of EOF is %d\n", EOF);
^ ^^
<<Remove the del for email>>

Mac
--
 
M

Mark McIntyre

To quote a certain Pop, engage brain. Notice the '5:' just before
the word 'error'. This might cause you to examine the 5th line of
your source.

Or equally it might mean it was version 5 of the file,under VMS. Not
that I dispute your interpretation, but its frequently dangerous to
second guess platform-specific error messages.
 
B

Barry Schwarz

ITYM:
printf("The value of EOF is %d\n", EOF);
^ ^^

Yes and no, in turn.

The first is a typo. I'm wondering how I did it since I used cut and
paste.

The second is unnecessary since all output streams are flushed when
main returns.


<<Remove the del for email>>
 
K

Kevin Goodsell

Barry said:
Yes and no, in turn.

The first is a typo. I'm wondering how I did it since I used cut and
paste.

The second is unnecessary since all output streams are flushed when
main returns.

Flushing is not the issue here. It is implementation-defined whether a
non-empty text stream must be terminated with a newline, therefore
portable code must terminate any output to a text stream with a newline.

In practice, some systems will not correctly display the last line of
output if it is not newline-terminated, and some systems require a valid
non-empty text file to end with a newline.

-Kevin
 
L

Lew Pitcher

shan said:
Hi,
I am trying to do the exercises in K & R and in ex 1-7
the problem is to find out the value of EOF. I wrote the
program given below, but an error is thrown during
compilation. I am using GCC in cygwin environment.
The program is:

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

The single equal sign ( = ) 'assigns' the value on it's right to the variable
on it's left

However, the double equal sign ( == ) compares the value on it's right to the
value on it's left, and results in a 1 if both values are equal or a 0 if
they are unequal.

Your statement is written incorrectly, as it uses an assignment equal sign
where it should have used an comparison equal sign.

The statement is

(val_eof=getchar())=EOF

and you are trying to assign the value of
EOF
to the expression
(val_eof=getchar())

Unfortunatly, an expression isn't something that you can assign a value to, so
the compiler complains about the statement.

You /meant/ to code
(val_eof=getchar()) == EOF
which would have /compared/ the value of the expression
(val_eof=getchar())
to the value of
EOF
and permitted the while() statement to act apon that comparison.


printf("The value of EOF is %d",val_eof);
return 0;
}


The error is

MurugesanSH@INP-Murugesans /cygdrive/d/shan-prog/cprog
$ gcc -o eof eof.c
eof.c: In function `main':
eof.c:5: error: invalid lvalue in assignment

Any help is appreciated
Brgds
Sha


--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
 
G

Goran Larsson

Mark McIntyre said:
Or equally it might mean it was version 5 of the file,under VMS.

No. The delimiter for a version number in VMS is semicolon, so for
it to mistaken as a VMS file it should have been eof.c;5.
 
B

Barry Schwarz

Flushing is not the issue here. It is implementation-defined whether a
non-empty text stream must be terminated with a newline, therefore
portable code must terminate any output to a text stream with a newline.

I believe what is implementation defined is whether a file requires
this \n. There are numerous examples in this group where the gurus
say printf can be used to prompt the user to enter data without a \n
(in order to keep the input on the same line as the prompt) as long as
it is followed by a flush of stdout.
In practice, some systems will not correctly display the last line of
output if it is not newline-terminated, and some systems require a valid
non-empty text file to end with a newline.

-Kevin



<<Remove the del for email>>
 
K

Kevin Goodsell

Barry said:
I believe what is implementation defined is whether a file requires
this \n. There are numerous examples in this group where the gurus
say printf can be used to prompt the user to enter data without a \n
(in order to keep the input on the same line as the prompt) as long as
it is followed by a flush of stdout.

The standard specifies this for text streams, not specifically files.

7.19.2 Streams

2 A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined. [...]
Data read in from a text stream will necessarily compare equal to
the data that were earlier written out to that stream only if: the
data consist only of printing characters and the control characters
horizontal tab and new-line; no new-line character is immediately
preceded by space characters; and the last character is a new-line
character.

I don't think the printf with fflush is relevant, since it does not
generally occur at the end of the stream. If it did occur at the end of
the stream (meaning stdout were closed afterward, without a terminating
newline), I believe the result would depend on the implementation.

-Kevin
 
P

Peter Shaggy Haywood

Groovy hepcat shan was jivin' on 22 Nov 2003 12:19:11 -0500 in
comp.lang.c.
newbie EOF question's a cool scene! Dig it!
I am trying to do the exercises in K & R and in ex 1-7

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

Well, that's extremely easy, one of the simplest things you could
possibly do. You seem to be confused about what you have to do,
though, and are making it more dificult than it has to be.
the problem is to find out the value of EOF. I wrote the

Nope, that's no problem at all. You don't even have to know the
value of EOF. Just pass EOF to printf(), and Bob's yer uncle.
program given below, but an error is thrown during
compilation. I am using GCC in cygwin environment.
The program is:

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

What on Earth are you doing that for? It's not even legal. You're
assigning the value EOF to an expression that is not an lvalue (an
object), but I think you mean to compare instead. This is a common
mistake. Remember, = (single equal sign) is for assignment but ==
(double equal sign) is for comparison. But I think you want to compare
for inequality, not equality. Then you need the != operator instead.
What you're trying to do is this:

while((val_eof = getchar()) != EOF)

But this still does something that is not in the problem
specification, which says "Write a program to print the value of EOF."
Well, where does it say to call getchar() or get user input? If it had
said, for example, "Write a program to print the value 42," what would
you do? No doubt you'd write a program that prints the value 42, and
does so without any user input, right? So when asked to write a
program to print the value of EOF, what are you gonna do?
printf("The value of EOF is %d",val_eof);

This is almost right. Well, it's right if val_eof does hold the
value of EOF. However, you can dispense with val_eof altogether, if
you wish, by simply using EOF itself. Otherwise, simply assign EOF to
val_eof.
You seem to have a (very common) misconception about what EOF
actually is. EOF is a macro defined in the stdio.h header, and has an
integer value. So, by virtue of that fact, printing its value is
exactly as easy as printing any integer value.
return 0;
}

The error is

MurugesanSH@INP-Murugesans /cygdrive/d/shan-prog/cprog
$ gcc -o eof eof.c
eof.c: In function `main':
eof.c:5: error: invalid lvalue in assignment

Learn to interpret the error messages emitted by your compiler. This
is an invaluable skill. These messages can be rather cryptic at times.
But this one is very easy to understand. It's saying that you're
trying to assign something to something that is not an object. The
compiler is even giving you its best estimate of where this has
occurred (function main(), file eof.c, line 5). I have shown you above
what the error is, so I won't dwell on it further.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
D

Dan Pop

In said:
I believe what is implementation defined is whether a file requires
this \n. There are numerous examples in this group where the gurus
say printf can be used to prompt the user to enter data without a \n
(in order to keep the input on the same line as the prompt) as long as
it is followed by a flush of stdout.

A genuine guru would argue that the flush of stdout is NOT necessary at
all before getting input from stdin ;-)

Dan
 
C

CBFalconer

Dan said:
.... snip ...
flush of stdout.

A genuine guru would argue that the flush of stdout is NOT
necessary at all before getting input from stdin ;-)

Chapter and verse time please.
 
M

Mike Wahler

CBFalconer said:
Chapter and verse time please.

printf("prompt");
getchar();

Data will be retrieved from stdin (assuming any is available).
Dan made no remarks about the output from 'printf()'. :)

-Mike
 
B

Barry Schwarz

A genuine guru would argue that the flush of stdout is NOT necessary at
all before getting input from stdin ;-)

The argument seems to be that under some allowable buffering
conditions, the prompt might not be presented to the user before the
input statement is executed and therefore he may not know what is
expected. Necessary, I guess not. Useful, I think so.


<<Remove the del for email>>
 

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

Similar Threads

problem with getchar EOF 2
Beginner at c 0
EOF question.. 2
how to print the value of EOF? 1
EOF question 41
How to input EOF? 6
getchar function and EOF problem.. 13
Question about EOF 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top