evaluating a expression

int k =8;
int b = (k++ -k++ - k++);

how this expression is evaluated?
closed account (zb0S216C)
It's hard to predict since the standard doesn't guarantee which increment expression is evaluated first.

Place a set or parentheses around each k++. Within the context of expressions, the uses of parentheses overrule the precedence of the used operators, meaning that the parenthesized expressions are evaluated first.

You should note the difference between N++ and ++N. N++ uses the value within N then increments it. ++N increments N then uses it. Your expression should look like this:

int B( ( K++ ) - ( K++ ) - ( K++ ) );

Wazzak
Place a set or parentheses around each k++. Within the context of expressions, the uses of parentheses overrule the precedence of the used operators, meaning that the parenthesized expressions are evaluated first.
What does that accomplish? ( ( K++ ) - ( K++ ) - ( K++ ) ) and (k++ -k++ - k++) are equivalent expressions.
Google around "sequence points". Parentheses do not indicate sequence points.
Topic archived. No new replies allowed.