tags:

views:

400

answers:

4
+13  A: 

It is unspecified which of the arguments to + is evaluated first - but that doesn't even matter, because in C and C++, modifying the same object twice without an intervening sequence point is completely undefined behaviour.

Here you're modifying x three times without an intervening sequence point, so you're well off the reservation ;)


The relevant part of the C99 standard is "6.5 Expressions":

2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

and

3 The grouping of operators and operands is indicated by the syntax. 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.


It's possible to write legal code that demonstrates the unspecified order of evaluation - for example:

#include <stdio.h>

int foo(void)
{
    puts("foo");
    return 1;
}

int bar(void)
{
    puts("bar");
    return 2;
}

int main()
{
    int x;

    x = foo() + bar();
    putchar('\n');

    return x;
}

(It is unspecified whether you get output of foobar or barfoo).

caf
Thanks for the quick reply.
Bill the Lizard
+2  A: 

The C standard does not guarantee that the post-increment will actually "happen" after the pre-increment. So that is undefined behaviour.

Earlz
+1  A: 

That's not a valid C statement, so it doesn't make sense to talk about the evaluation order.

Stephen Canon
What do you mean by "valid"? It successfully compiles in gcc and g++.
Andrew Medico
Whether or not a file is successfully compiled by gcc has nothing to do with whether or not it contains valid C statements. The C standard determines what is valid C, not one compiler.
Stephen Canon
This statement is syntactically valid, although semantically incorrect as it causes undefined behaviour.
qrdl
+3  A: 

C++03 Standard 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.53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

... hence, undefined and implementation dependant.

Kornel Kisielewicz