This is "undefined behaviour". It could do anything at all. Do not do this. The C++ standard explicitly states that when you mix increment operators like this, there's no way to know what will happen.
I agree completely, but on one of my exams in my c class, our teacher put crap like this on our exam. Thus we had to learn the precedence operations of such stupidity.
And yay, the holy book (2003 edition) did state, in Section 5 Paragraph 5:
"Between the previous and next sequence point a scalar object shall have its value modified at most once by the evaluation of an expression.... otherwise the behaviour is undefined."
Piecing together what this compiler chose to do in this situation does not change this.
its not really undefined behaviour
in operator precedence, it works left to right (excluding the = operator)
C++ + ++C first, but the first C++ doesnt increment till after the operation
thus 11 + 12 = 23
then c increments due to the post operator, making it 13, then the pre increment making it 14
then we have 23 + 14 = 37
It doesn't make sense. Take c++ + ++c. You say the '++c' increments before the addition, but after c++ ('at pass'). To be consistent, c++ should have incremented after c++, but before ++c, making the total solution 11 + 13 + 14 = 38.
Now, c++ apparently incremented after the entire sum thing, mimicking this behaviour:
1 2
a = c + ++c + ++c;
c++;
To be consistent, ++c should have incremented before the start of the sum, to get this behaviour:
1 2 3 4
++c;
++c;
a = c + c + c;
c++;
In my eyes, this shows that whoever designed the compiler didn't account/care for mixed pre/post incrementing. Because he shouldn't have. Because it's undefined behaviour.
as for everyone else, this is what i learned in my class... i assumed thats what he was going for too, i agree that this is unpredictable in a computer operation, but if you systematically go through and do the operations, it makes sense to me at least.. and i got a 100 on that exam, so i learned something...
and i got a 100 on that exam, so i learned something...
There was a question like this on the exam and the correct answer answer for which marks were awarded wasn't "it's undefined"?
but if you systematically go through and do the operations, it makes sense to me at least
That's great for you, but you're not a C++ compiler conforming to the C++ standard, are you? Whatever "system" you mean when you say "systematically" is not C++.
>> That won't even compile. Not exactly sure why, though
The postfix increment operator has a higher precedence than the prefix increment operator.
So ++a++ is evaluated as ++( a++ )
Assuming that a standard type is involved, a++ results in an r-value; the prefix increment operator requires a modifiable l-value.
Note: (++a)++ would compile; the prefix increment operator which is evaluated first yields a modifiable l-value.