Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would the evaluation order of x = x++ + ++x; be? [duplicate]

Tags:

c++

c

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

In Java the evaluation order is specified to be left-to-right. Is this the case for C and C++ as well, or is it implementation dependent? I do remember that the evaluation order is unspecified for function arguments, but what about sub-expressions?

like image 273
Bill the Lizard Avatar asked Mar 03 '10 00:03

Bill the Lizard


People also ask

What is the order of evaluation in the expression?

"Order of evaluation" refers to when different subexpressions within the same expression are evaulated relative to each other. you have the usual precedence rules between multiplication and addition.

What is the order of evaluation of the operators?

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.

What is a redex in lambda calculus?

A redex, or reducible expression, is a subexpression of a λ expression in which a λ can be applied to an argument. With more than one redex, there is more than one evaluation order. e.g. (+(* 3 4) (* 7 6)). Normal Order Evaluation. Always reduce leftmost redex.


2 Answers

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 into here be dragonnes territory ;)


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).

like image 93
caf Avatar answered Sep 24 '22 01:09

caf


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.

like image 30
Kornel Kisielewicz Avatar answered Sep 23 '22 01:09

Kornel Kisielewicz