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.

Xeo
  • 129,499
  • 52
  • 291
  • 397
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • +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. – Alok Save Aug 18 '11 at 19:00
  • @Als: didn't downvote it yet, but some research (http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) would have been good. – woliveirajr Aug 18 '11 at 19:06

6 Answers6

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.

Xeo
  • 129,499
  • 52
  • 291
  • 397
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 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.5 –  Aug 18 '11 at 18:34
  • @Code Monkey: Oops, Added that quote belongs to C++03. – Alok Save Aug 18 '11 at 18:35
  • 1
    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." –  Aug 18 '11 at 18:35
1

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

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

Krenair
  • 570
  • 5
  • 21
Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • 4
    Its not *undefined*. Its *unspecified*. The Standard makes a difference between these two terminologies. – Nawaz Aug 18 '11 at 18:42
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.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
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.

dbush
  • 205,898
  • 23
  • 218
  • 273
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.

Community
  • 1
  • 1
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55