# About precedence of && and ||

• 09-10-2010
noobiept
About precedence of && and ||
Hi, while I was helping in some programming exercises, there was this problem:
Code:

```int i = 1, j = 1, k = 1; printf("%d ", ++i || ++j && ++k); printf("i:%d j:%d k:%d", i, j, k);```
I checked the precedence table of c operators, and saw that && has higher precedence than ||, so I guessed that the output for the variables would be: i:2 j:1 k:2 (j would be short-circuited).

But when we tested it, the output was: i:2 j:1 k:1.
Basically the || was evaluated first, and I didn't understand why.

What's going on?
• 09-10-2010
Salem
The precedence is telling you that you've got this

++i || ( ++j && ++k )

and not this

( ++i || ++j ) && ++k
• 09-10-2010
noobiept
I still don't get it, because the || is been evaluated first. Doesn't having higher precedence means that it is evaluated first? This way the && isn't even considered.
• 09-10-2010
CommonTater
Quote:

Originally Posted by noobiept
I still don't get it, because the || is been evaluated first. Doesn't having higher precedence means that it is evaluated first? This way the && isn't even considered.

First the double || and && are for use in conditionals, not math. For example:

if ((x > 6) || (y < 7))

If you are doing math you use the single versions not the doubles.

For order or precidence the general rule is:
Deepest brackets first, working out.
Once at top level, left to right.

Code:

```printf("%d ", ++i | ++j & ++k); printf("i:%d j:%d k:%d", i, j, k);```
If you want the AND to run first put brackets like this:
Code:

```printf("%d ", ++i | (++j & ++k)); printf("i:%d j:%d k:%d", i, j, k);```
Does that help?
• 09-10-2010
iMalc
Quote:

Originally Posted by noobiept
I still don't get it, because the || is been evaluated first. Doesn't having higher precedence means that it is evaluated first? This way the && isn't even considered.

Operator precedence is a different concept from order of evaluation. The two are very different things. One is how to build the syntax tree, the other is how to evaluate it.

Operator precedence says that our syntax tree for this is the left one of these two:
Code:

```    ||                &&    /    \            /    \ ++i    &&        ||    ++k       /  \      /  \          ++j ++k    ++i ++j```
But it says nothing about the order the parts of the tree are evaluated.

The order of evaulation in this case says to go from left to right, but short circuit evalution says to skip the right hand side when the left of an || is true or the left of an && is false. So we do ++i and then the || at the top of our tree is satisfied and we therefore stop.
• 09-10-2010
noobiept
Ok, I was assuming that operator precedence would determine the order of evaluation. Didn't know it was something different.
Guess I learned something today :)

Just a last question, how do we know that the expression is evaluated from left to right? Is it compiler dependent or something?
• 09-10-2010
zacs7
Quote:

Originally Posted by CommonTater
First the double || and && are for use in conditionals, not math. For example:

if ((x > 6) || (y < 7))

If you are doing math you use the single versions not the doubles.

For order or precidence the general rule is:
Deepest brackets first, working out.
Once at top level, left to right.

Code:

```printf("%d ", ++i | ++j & ++k); printf("i:%d j:%d k:%d", i, j, k);```
If you want the AND to run first put brackets like this:
Code:

```printf("%d ", ++i | (++j & ++k)); printf("i:%d j:%d k:%d", i, j, k);```
Does that help?

| and & are just as mathematical as && and ||. I don't see what bitwise operators have to do with anything the OP was asking.

Quote:

Originally Posted by noobiept
Just a last question, how do we know that the expression is evaluated from left to right? Is it compiler dependent or something?

It's defined by the standard. A quick google for "C operator table" will probably give you good results (from a reliable source, preferably the standard :-)).
• 09-10-2010
noobiept
I was referring to the order of evaluation, and not the operator precedence (or maybe I'm confusing myself?)
• 09-10-2010
Bayint Naung
++i || ++j && ++k
++i || (++j && ++k) // && and higher precedence
2 || (++j && ++k) // left to right evaluated
2 || whatever_exp // is always true , now short-circuit! whatever_exp is not evaluated!

So j and k are never incremented. Got it?
• 09-10-2010
new_ink2001
noobiept,

According to Wang, Paul S. An Introduction to ANSI C on UNIX Boston: PWS 1993. ISBN: 0-534-14232-X, operator precedence is given on page 397 as, and the associativity is (always) left to right unless otherwise marked:

Code:

```Operator                                Associativity () [] -> . !~ ++ -- + - * & (type) sizeof                <---- * / % + - << >> < <= > >= == != & ^ | && || ?:                                        <---- = += -= *= /= %= ^= |= <<= >>=                <---- ,```
These are also listed on the Wikipedia: Operators in C and C++ - Wikipedia, the free encyclopedia.

Best Regards,

New Ink -- Henry
• 09-12-2010
noobiept
I think you didn't understand what I was saying, but either way I search a bit and read about it.