Hello,
What's the order of precedence and associativity of the next expression:
while (scanf("%d", &n) == 1 && n >= 0)
your help will be appreciated
Thank you.
Hello,
What's the order of precedence and associativity of the next expression:
while (scanf("%d", &n) == 1 && n >= 0)
your help will be appreciated
Thank you.
&& is evaluated from left to right
if the left part is false - right part is not evaluated
what else do you need to know about this expression?
BTW http://www.lmgtfy.com/?q=C+precedence
Last edited by vart; 03-06-2009 at 04:02 PM.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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
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
some more paranthesesCode:while ((scanf("%d", &n) == 1) && (n >= 0))
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
As the 1999 edition of C standard says:Originally Posted by mashour06
Hence you should refer to vart's post #4 as to the order of evaluation since operator && is involved.Originally Posted by C Standard, 1999 edition, section 6.5, paragraph 3
No, operators >= and == have precedence over operator &&. Incidentally, operator >= has precedence over operator ==, but that is not relevant in this case.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for you all.