Short circuit evaluation of a statement with ++ operator in C
Asked Answered
F

2

15

I have executed the following code in Code::Blocks 10.05 on Windows 7.

int a=0,b=0,c;
c=a++&&b++;
printf("\na=%d\nb=%d\nc=%d\n\n",a,b,c);

The output I obtained is given below,

a=1
b=0
c=0

This makes perfect sense because of short circuit evaluation.

The expression a++ is post increment and 0 is returned to the logical and (&&). Hence the part b++ is not evaluated since both 0 && 0 and 0 && 1 evaluates to 0.

But here arises my doubt. The precedence value of operators clearly states that ++ is having higher precedence over &&. So my understanding was like this, both a++ and b++ are evaluated and then && only checks the result of expression a++ to come to a decision. But this has not happened only a++ is evaluated here.

What is the reason for this behavior? Does && being a sequence point has something to do with this behavior? If so why we say that && is having lower precedence than ++?

Fennel answered 3/8, 2015 at 4:24 Comment(6)
there is one more rule that is above them, a++ will only increment after a statement has executed. So, whatever its precedence is, its going to be incremenmted after statment is finished.Sandusky
Wow, this is a nasty trap.Arnelle
Apart from the interesting language aspects of this question, I would recommend making your code immediately legible and obvious. At present this is not the case.Leong
I think it's sad that such code gets ANY upvotes.Bobwhite
@saurabhagarwal a++ will increment at some indeterminate time between when a is evaluated and when the statement finishes. That's why you can't use a again within the same expression (before or after the a++).Judges
To be honest, I think it's sad that we've had two people complaining about the code instead of editing the post. (To hold off obvious responses: I'll leave prettifying to people who actually work with the language.)Tap
B
36

You are confused about precedence and order of evaluation.

Precedence defines how the operators are grouped, i.e

c = a++ && b++;

is equivalent to:

c = ((a++) && (b++));

Order of evaluation defines how the expression is evaluated, the short circuit of && means a++ is evaluated first, if it's zero, the end; if it's not zero, b++ is then evaluated.


As another example:

c = (a++) + (b++);

Is a++ evaluated before b++? The answer is we don't know. Most operators don't define the order of evaluation. && is one of the few operators that do define. (The rest are ||, , and ?:)

Blood answered 3/8, 2015 at 4:32 Comment(1)
Another way to say this is that && defines a sequence point after the evaluation of its left argument and before that of its second argument. There is no sequence point defined for +. Wikipedia has a good article about sequence points.Calices
S
4

There are two concepts here - order of precedence and order of evaluation. Order of precedence will have an impact only if an expression (or sub-expression) is evaluated.

In general, the order of evaluation is not sequenced. Given an operator, its operands can be evaluated in any order. The arguments of a function can be evaluated in any order.

From the C++ Standard:

1.9 Program execution

15 Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.

and

8.3.6 Default arguments

9 Default arguments are evaluated each time the function is called. The order of evaluation of function arguments is unspecified.

For the logical AND operator, &&, the C++11 standard says:

5.14 Logical AND operator

1 The && operator groups left-to-right. The operands are both contextually converted to type bool (Clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

Similar exception is specified for the logical OR operator, ||.

Since b++ is not evaluated due to short circuiting of the expression because of && operator, the order of precedence of the operators has no significance in this particular case.

Shoal answered 3/8, 2015 at 4:32 Comment(1)
saying "order of precedence" confuses the issue, "order of evaluation" and "precedence" need to be kept separateSoul

© 2022 - 2024 — McMap. All rights reserved.