They aren't the same.
c--
is a postfix operator which changes
c
after it's been used.
You need to be very careful when using these, as the results you get may not be what you expected. See here:
Why does x = ++x + x++ give me the wrong answer?[
^]
Quote:
Thank you so much! I've read an article and found the "Precedence and Order of Evaluation" from MSDN.
For the moment the precedence of operators doesn't answer the question "why"
for example
int a = 5;
int b = 3;
int i = 10;
int x = (i++) + a * b;
logically (if we agree with precedence) i should be 11 (!) and x = 26, not 25, because i++ is placed in brackets.
but the ++ operator is still omit eventually, no matter on evaluation direction
No, you are wrong.
int x = i++ + a * b;
Is using a post fix increment, which says, "use the current value of
i
and then add one to
i
"
If you write it long hand, you get this:
int i2 = i;
i += 1;
x = i2 + a * b;
which is:
25 == 10 + 3 * 5;
That's
why mixing auto increment and decrement operators in the same expression is so fraught: there are no universal rules to say exactly when the increment should be done.
Think of this, assuming i is 10
x = i++ + i;
What is the value of x?
20?
21?
22?
The "right" answer is 21, and i ends up as 11.
int i2 = i;
i += 1;
x = i2 + i;
But ... it could easily be 20 if strictly evaluated from right to left:
x = i + i;
i += 1;