-2

Here's the problem:

With int a,b,c;, is a = a * b + a; equivalent to (c = a * b)!=(a = c + a); with respect to modification of a?

At first glance I think they are the same, but then does specification say that in expr1 != expr2, expr1 will always be evaluated before expr2? I think it's not the case but I can't find a definitive source on this.

Also, I'm baffled by the what I found in http://en.cppreference.com/w/c/language/eval_order :

The side effect (modification of the left argument) of the direct assignment operator and of all compound assignment operators is sequenced after the value computation (but not the side effects) of both left and right arguments.

(since C11)

Does this mean I can interpret it as since C11 the above statement will have fixed evaluation order (since assignment is involved), but not pre-C11?

Community
  • 1
  • 1
Virgil Ming
  • 549
  • 4
  • 12
  • 1
    Not super keeping up with the standards, but AFAICT the "since C11" part refers to the "sequenced after" formulation; Earlier standards used a concept of "sequence points" that isn't used anymore. Morally there shouldn't be any huge difference between the two statements in C99 and C11; Standards upgrades, _especially_ in the case of the C and C++ languages, don't really break existing code conforming to older standards with rare exceptions. – Cubic Jan 07 '17 at 13:01
  • There is no sequence point for the last -> UB. – too honest for this site Jan 07 '17 at 13:06
  • @Cubic: So why does the standard mentions sequence points all over the text and even provides an annex with the sequence points? They did not change that, do you mean the **different** language C++? "sequenced after" is not the same in C as sequence points. – too honest for this site Jan 07 '17 at 13:07
  • @Olaf It was just a guess really. I was hoping standards committees were trying to keep language at least somwhat consistent between C and C++ standards. Must be a nightmare to keep up with both then. – Cubic Jan 07 '17 at 15:14
  • @Cubic Why a nightmare? You don't expect Python (or Pascal to pick a officially standardised language) and Fortran to be in sync either, do you? – too honest for this site Jan 07 '17 at 15:56

1 Answers1

2

(c = a * b)!=(a = c + a); invokes undefined behavior. The order of evaluation of expressions (c = a * b) and (a = c + a) is not sequenced.

C11-6.5/2:

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.[...]

The quotation

The side effect (modification of the left argument) of the direct assignment operator and of all compound assignment operators is sequenced after the value computation (but not the side effects) of both left and right arguments.

means that for the expressions like a = b; or a += b, assignment of b to a is sequenced after the evaluation of the subexpressions a and b in both statements. This is guaranteed by C standard. But, order of evaluation of a and b in the above statements is not guaranteed.

haccks
  • 104,019
  • 25
  • 176
  • 264