order of evaluation of operands [duplicate]
Asked Answered
T

6

16

In the expression a + b, is a guaranteed to be evaluated before b, or is the order of evaluation unspecified? I think it is the latter, but I struggle to find a definite answer in the standard.

Since I don't know whether C handles this different from C++, or if evaluation order rules were simplified in C++11, I'm gonna tag the question as all three.

Tetrad answered 18/8, 2011 at 18:28 Comment(2)
+1 Because I don't see any reason for this to be downvoted. Infact this might serve as a very good faq, where we have relevant quotes from c99, c++03 and c++11 standards at one place.Individualism
@Als: didn't downvote it yet, but some research (#4176828) would have been good.Sitdown
M
13

In C++, for user-defined types a + b is a function call, and the standard says:

§5.2.2.8 - [...] The order of evaluation of function arguments is unspecified. [...]

For normal operators, the standard says:

§5.4 - Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. [...]

These haven't been changed for C++11. However, the wording changes in the second one to say that the order is "unsequenced" rather than unspecified, but it is essentially the same.

I don't have a copy of the C standard, but I imagine that it is the same there as well.

Mckinley answered 18/8, 2011 at 18:38 Comment(1)
CodeMonkey, already poosted the quote from the c99 standard as an comment under my answer.Individualism
I
11

It is Unspecified.

Reference - C++03 Standard:

Section 5: Expressions, Para 4:

except where noted [e.g. special rules for && and ||], the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is Unspecified.

Individualism answered 18/8, 2011 at 18:32 Comment(3)
5.2 does not seem like the right section... I am looking at it now - edit oops I am looking at C99 spec it is 6.5Livvy
@Code Monkey: Oops, Added that quote belongs to C++03.Individualism
In C99 it is at 6.5. "The grouping of operators and operands is indicated by the syntax.72) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified."Livvy
I
1

It is unspecified. C and C++ follow the same logic in selecting sequence points:

https://en.wikipedia.org/wiki/Sequence_point

Immortalize answered 18/8, 2011 at 18:34 Comment(1)
Its not undefined. Its unspecified. The Standard makes a difference between these two terminologies.Monkhmer
T
1

C++0x FDIS section 1.9 "Program Execution" §15 is similar to the corresponding paragraph in C++03, just reworded to accommodate the conceptual change from "sequence points" to "being sequenced":

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

Tetrad answered 18/8, 2011 at 18:46 Comment(0)
H
0

For C: "Order of operations is not defined by the language. The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result." [...] "Only the sequential-evaluation (,), logical-AND (&&), logical-OR (||), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands."

Source: http://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx

The way this is organized in that article, it seems to indicate this applies to C++ too, which is confirmed by the answer to this question: Operator Precedence vs Order of Evaluation.

Handrail answered 18/8, 2011 at 18:31 Comment(0)
B
0

According to the current C standard, C11, it also specifies that the order of evaluation of subexpressions (a and b in this case) is indeterminate. In fact, this order doesn't even have to be the same if the same expression is evaluated multiple times.

From section 6.5:

The grouping of operators and operands is indicated by the syntax.85) Except as specified later, side effects and value computations of subexpressions are unsequenced.86)


86) In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations.

Boeke answered 3/12, 2016 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.