jeremymh
08-14-2003, 11:18 PM
I've just started reading the book "The C Programming Language (http://www.amazon.com/exec/obidos/tg/detail/-/0131103628/103-4790363-3842269) ". This edition, although the newest, is around 15 years old. Although I've been told it's up to date (as it is ANSI C)
I've gotten to an example that goes like this
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
This seems to work alright, as any text I type appears on screen (except I have to ctrl-c out of the program as I don't know how to put the EOF on the keyboard)
Then he mentions that you need the brackets around the c = getchar() because the != will be done before the =
And he suggests that we try it without the brackets to see what happens (it should output a 1 or a 0)
Unfortunately the program didn't seem to act any differently. I tried the following code, just to see if the compiler has a different order to what he defines, but it still acts exactly the same.
#include <stdio.h>
main()
{
int c;
while (c = (getchar() != EOF))
putchar(c);
}
I'm using gentoo linux, bash, gcc (compiling with "cc gettext.c" and running it as the a.out exec) which all should be the latest stable version gentoo has in portage.
Can anyone shed some light on the situation? is it me, the book, gcc, bash or what?
I've gotten to an example that goes like this
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
This seems to work alright, as any text I type appears on screen (except I have to ctrl-c out of the program as I don't know how to put the EOF on the keyboard)
Then he mentions that you need the brackets around the c = getchar() because the != will be done before the =
And he suggests that we try it without the brackets to see what happens (it should output a 1 or a 0)
Unfortunately the program didn't seem to act any differently. I tried the following code, just to see if the compiler has a different order to what he defines, but it still acts exactly the same.
#include <stdio.h>
main()
{
int c;
while (c = (getchar() != EOF))
putchar(c);
}
I'm using gentoo linux, bash, gcc (compiling with "cc gettext.c" and running it as the a.out exec) which all should be the latest stable version gentoo has in portage.
Can anyone shed some light on the situation? is it me, the book, gcc, bash or what?