Thread: while (scanf("%d", &n) == 1 && n >= 0)

1. while (scanf("%d", &n) == 1 && n >= 0)

Hello,
What's the order of precedence and associativity of the next expression:
while (scanf("%d", &n) == 1 && n >= 0)
Thank you.

2. && is evaluated from left to right
if the left part is false - right part is not evaluated

BTW http://www.lmgtfy.com/?q=C+precedence

3. isn't the order as follow (according to a table of precedence and associativity):
scanf() is called first because of its highest precedence.
second: n >= 0 is evaluated
third:scanf() return value is compared to 1
fourth: && is invoked

4. Originally Posted by mashour06
isn't the order as follow (according to a table of precedence and associativity):
scanf() is called first because of its highest precedence.
second: n >= 0 is evaluated
third:scanf() return value is compared to 1
fourth: && is invoked
right part of && is evaluated only after left part, and only if left part is true

5. In this case I think the left part will always be true because && has precedence and the left term is just 1. To make this work properly you need
Code:
`while ((scanf("%d", &n) == 1) && (n >= 0))`
some more parantheses

6. Originally Posted by mashour06
isn't the order as follow (according to a table of precedence and associativity):
As the 1999 edition of C standard says:
Originally Posted by C Standard, 1999 edition, section 6.5, paragraph 3
The grouping of operators and operands is indicated by the syntax. Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
Hence you should refer to vart's post #4 as to the order of evaluation since operator && is involved.

Originally Posted by MK27
In this case I think the left part will always be true because && has precedence and the left term is just 1.
No, operators >= and == have precedence over operator &&. Incidentally, operator >= has precedence over operator ==, but that is not relevant in this case.

7. Thank you

Thanks for you all.