# A good question about precedence

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 ...

1. ## A good question about precedence

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 ?

2. I rate this question as "average".

You are missing a `-' in what I assume is supposed to be "--b".

Soma

3. Originally Posted by phantomotap
I rate this question as "average".

You are missing a `-' in what I assume is supposed to be "--b".

Soma
yes you are right it should be --b

4. This question has nothing to do with precedence or associativity.

Look around for articles on "short circuit operators" and find an answer.

Soma

5. 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?

6. Originally Posted by QuantumPete
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?

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

7. 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

8. Originally Posted by phantomotap
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.

The binary boolean logic operator "AND" has a higher precedence than binary boolean logic operator "OR".

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.

Soma
thanks a lot for your explanations.According to you what is the final values of a,b and c ?

9. 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.

Now, what do you think they should be and why?

Soma

10. Originally Posted by dayanike
According to you what is the final values of a,b and c ?
Why not write a program and find out for yourself?

11. I've already did dont worry but I try to understand whather ı misunderstood or the textbook is wrong.

12. I wrote what i think in my first post check it. It should be b=9 and c=9.

13. O_o

I can't make this any more clear: your interpretation of the condition is correct while your expectation of the values is wrong.

Soma

14. 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.