EOF & precedence of !=

Gurus,

I am teaching myself C and have a question.

I wrote a small prog that reads characters as entered at the prompt and checks the value for EOF.

Unless I am 100% wrong, the value will be '1' until getchar() has anything to read in my stream.

/* PROG 1 */
#include <stdio.h>

main()
{
int c;

    while \(c = getchar\(\) != EOF\)
            printf\("%d\\n", c\);

}

I compile this code, I run it at the prompt and type a string "1234<enter>" and get this output
1
1
1
1
1

So far, so good, right? For grins, I changed the number of brackets in my while() clause like below:

/* PROG 2 */
...
while ((c = getchar()) != EOF)
...

I re-compile this code and run it: at the prompt, I type the same string "1234<enter>" and get this output
49
50
51
52
10

Now, I obtain the decimal values for the stream I entered (line feed included).

Can someone please explain what is going on?

Thanks in advance! Al.

Based on the title of the thread, you seem to know what's happening. But to spell it out....

You are in effect doing....

c = (getchar() != EOF)

The expression (a != b) will evaluate to 1 if a is not equal to b. If they are equal it evaluates to 0.

I think I realized what I am doing...

Without brackets, the `...!= EOF...` part of the expression is evaluated first. That's why it is equal to 1 and displays accordingly with printf("%d\n", c);

With the brackets, the `...getchar()...` part of the expression is evaluated first. Consequently, EOF is not equal to 1, getchar() does its job and scans the streams, the decimal values for the character in my stream display accordingly with printf.

Am I making sense?

Not exactly. getchar() does its work in either case. First it gets that "49". Then we test the 49 to see if it is EOF. It's not. So the (49 != EOF) evaluates to 1. Now we store that 1 in the variable c.

When you put the parenthesis in, getchar() gets that 49. Then we store the 49 in "c". Now we test to see if c is EOF.

Thanks for your reply.

For what it is worth, I want to say that I greatly appreciate your effort and patience in answering my (stupid/basic...) questions.

Thanks again!

Al.