Operator precedence has nothing to do with order of evaluation. Precedence is the priority for grouping different types of operators with their operands.
So, the expression
++a || ++b && ++c;
will be evaluated as
++a || (++b && ++c);
Logical AND and Logical OR operator constitute sequence points and therefore guarantee a particular order of evaluation for their operands which is left to right.
Order of evaluation:
Ordering
......
- If a sequence point is present between the subexpressions E1 and E2,
then both value computation and side effects of E1 are
sequenced-before every value computation and side effect of E2
Rules
.....
2) There is a sequence point after evaluation of the first (left) operand and before evaluation of the second (right) operand of the following binary operators: && (logical AND), || (logical OR), and , (comma).
Logical OR operation (expr1 || expr2)
employs short-circuiting behavior. That is, expr2
is not evaluated if expr1
is logical 1
(true)
.
The initial value of a
, b
and c
is 0
. In the expression:
++a || ++b && ++c;
++a
-> pre-increment a
.
That means, the value of the expression ++a
is resulting incremented value of a
which will be 1
. Since, ||
operator employs short-circuit behavior, the right hand side expression of ||
will not be evaluated. Hence, you are getting output - 1 0 0
.
For better understanding, just try to change the ++a
-> a++
in the expression.
The post increment operator also increase the value of operand by 1
but the value of the expression is the operand's original value prior to the increment operation. So, a++
will be evaluated to 0
and because of short-circuit behavior the right hand side expression of ||
operator (++b && ++c
) will be evaluated.
The logical AND operation (expr1 && expr2
) also employs short-circuiting behavior. With logical short-circuiting, the second operand, expr2
, is evaluated only when the result is not fully determined by the first operand, expr1
. That is, expr2
will be evaluated only if expr1
is logical 1
(true)
and ++b
will result in 1
. So, if you do
a++ || ++b && ++c;
^^^
The output would be - 1 1 1
.