Hi guys here is my question:
let a=b=c=10 and what is the value of a,b,c after --a||--b&&--c
I think it should have been b=9 and c= 9.I think it should follow this pattern: ((--a)||((--b)&&(--c)))
is there anyone who can help me ?
This is a discussion on A good question about precedence within the C Programming forums, part of the General Programming Boards category; Hi guys here is my question: let a=b=c=10 and what is the value of a,b,c after --a||--b&&--c I think it ...
Hi guys here is my question:
let a=b=c=10 and what is the value of a,b,c after --a||--b&&--c
I think it should have been b=9 and c= 9.I think it should follow this pattern: ((--a)||((--b)&&(--c)))
is there anyone who can help me ?
Last edited by dayanike; 03-13-2012 at 06:35 AM.
I rate this question as "average".
You are missing a `-' in what I assume is supposed to be "--b".
Soma
This question has nothing to do with precedence or associativity.
Look around for articles on "short circuit operators" and find an answer.
Soma
phantomotap is right, this question has nothing to do with operator precedence.
And what exactly do you want help with? Does your answer not match the textbook?
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
What i tried to say is because of the higher precedence of && operator,firstly (--b)&&(--c) should have been executed and then the result of (--b)&&(--c) match with --a.I mean after all operations b should be 9 c should be 9 but the textbook says the evaluation proceeds from left to right although && has higher precedence than || so according to textbook a=9, b=10 and c=10
O_o
Well, your book is either wrong or, much more likely, you misunderstood.
Operators precedence and associativity are simply two different things.
The binary boolean logic operators are left to right associative: (first_here && then_here).
The binary boolean logic operator "AND" has a higher precedence than binary boolean logic operator "OR": (((stuff_here) && (stuff_there)) || (now_here)).
These things are both important; they don't have anything to do with each other.
You could read the above by duplicating semantics for a possibly better explanation of what you aren't getting.
The binary boolean logic operator "AND" is left-to-right associative and has a higher precedence than binary boolean logic operator "OR" which also is left-to-right associative: (first_here || (then_here && and_here)).
Soma
Last edited by phantomotap; 03-13-2012 at 07:46 AM. Reason: context
I'm not going to answer that question.
Answering an instance of a question will do you no good. I don't ........ing care to waste my time in that fashion.
I'm only posting to help you understand why you don't know the answer to it.
Now, what do you think they should be and why?
Soma
Why not write a program and find out for yourself?Originally Posted by dayanike
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I've already did dont worry but I try to understand whather ı misunderstood or the textbook is wrong.
I wrote what i think in my first post check it. It should be b=9 and c=9.
O_o
Read post 4!
I can't make this any more clear: your interpretation of the condition is correct while your expectation of the values is wrong.
Soma
The result is:
a = 9, b = 10, c = 10 because the expression overall is "logical". It is evaluated only as far as required to determine if the result will be true or false. After the first --a, the intermediate result is 9 which is considered TRUE (non zero). Since the connecting operation is logical OR, the entire expression is already TRUE no matter what follows. Therefore there is no need to evaluate more terms... and therefore the 'b' and 'c' variables are untouched.