thanks everybody!
I was wondering if the result is explainable in terms of operator
precedence and pre/post incre/decre-ment.
If a silly recruiter asks this as an interview question, he might
expect a definite answer.
No, I am treating this esteemed group for solving interview
questions.. please don't take above statement in that perspective
My intention is completely to address precedence and pre/post incre/
decre-ment in tricky situations (such as very well thought of
interview questions to test specifics of a language).
I don't think you 'got' the answers you were given. You example is NOT
explainable in terms of the C++ language. It is UNDEFINED BEHAVIOUR as
far as C++ goes.
Of course th authors of which ever compiler you were using might have an
explanation about why they made their compiler do that, but that is a
question for the compiler writers, not a question for C++. And not a
very interesting question either.
If you want to pose question about operator precedence and pre and post
increment you should stick to questions where the C++ language defines a
particular behaviour.
I only understood these issues when I realise that operators can have a
value and a side effect. For ++x the value is x + 1, and the side effect
is to increment x, for x++ the value is x, and the side effect is also
to increment x. That's the only difference. Where newbies go wrong is
that they think pre and post increment have something to do with the
TIMING of when the increment operation happens, but they don't. Pre and
post-increment operators are evaluated using exactly the same rules as
any other operator, it is not the case that pre-increment happens
'first' and post-increment happens 'later' or any such thing.
john