Win a copy of Design for the Mind this week in the Design forum!

# operator precedence

d jones
Ranch Hand
Posts: 76
Hi,

In operator precedence the postfix operator apparently has a higher priority than the additive operators. However, if we have:

int c = 10;
int d = 5 + c++;
d equates to 15 - which is what I would expect because the postfix operator is only applied when the entire expression has been resolved.

I don't understand why then the postfix operators come before the additive operators on the operator precedence list.

Could anyone explain?

Many Thanks

Christophe VerrĂ©
Sheriff
Posts: 14691
16
The problem here is that c++ is evaluated first, but it returns the value of c before begin incremented.
Compare
int d = 5 + (c++); // 5 + 10
and
int d = 5 + (++c); // 5 + 15

Same as this :
http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment

kwan Jang
Greenhorn
Posts: 16
int d = 5 + (++c); // 5 + 15?
why?
not int d = 5 + (++c); // 5 + 11 ???

Christophe VerrĂ©
Sheriff
Posts: 14691
16
Typo. Thanks for pointing it out kwan.

Chetan Raju
Ranch Hand
Posts: 109
Although increment operators come higher up in the hierarchy of precendence table, the order of evaluation also matters. for unary increment operators, I suppose the order of evaluation is from Right to Left.