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"?