"C - C++" joke about postfix/prefix operation ordering
My friend sent me a joke:
Q. What's the difference between C and C++?
A. Nothing, because: (C - C++ == 0)
I tried to change order and got stuck.
Loo开发者_JS百科k at this code:
public class Test {
public static void main(String args[]) {
int c = 10;
System.out.println(c++ - c);
System.out.println(++c - c);
}
}
Why does it return:
-1
0
I understand postfix and prefix increment. Why isn't this the result?
0
1
Because in the first example, c
starts out 10. c++
increments c
and returns 10, so the second c
now evaluates to 11 since it was incremented. So the ultimate expression evaluated is 10 - 11
, which equals -1.
In the second example, ++c
increments c
again but returns 12 since it is a pre-increment. The second c
evaluates to 12 as well, since it's the new value stored in c
. So that expression ultimately is evaluated as 12 - 12
, which equals 0.
Because c++ - c
becomes (c - c-incremented-by-one)
, which is -1 and ++c - c
becomes (c-incremented-by-one - c-incremented-by-one)
which is 0.
The difference is in the operators you use ++c and c++
c++ will increment the value BEFORE it is used in the calculation ++c will increment the value AFTER it is used in the calculation
also cdhowie's answer explains it better
Whether it outputs 0 or -1 for the first println and 0 or 1 for the second is undefined. The compiler is at liberty to evaluate the left or right hand side first and so whether the increments have taken effect by the time the right hand side is evaluated will depend on the compiler (and indeed the compiler could do one thing one time and another the next)
The increments are only guaranteed to take place at the next sequence point. Subtraction is not a sequence point.
EDIT: Bah, wrote that answer when the question was tagged C/C++. Sequence points don't apply to Java. I've left the answer in case others find it useful when considering C/C++ expressions
Because the increment is performed before the operation in one case, whereas the increment is performed after the operation in the other case.
int c = 10;
System.out.println(c); // output 10
System.out.println(c++); // outputs 10
System.out.println(c); // output 11
System.out.println(++c); // output 12
System.out.println(c); // output 12
That's the difference between postfix and prefix increment.
Here's a nice text on that:
http://www.java-samples.com/showtutorial.php?tutorialid=249
"Both the increment operator (++) and the decrement operator(--) come in two varieties: prefix and postfix. The prefix variety is written before the variable name (++myAge); the postfix variety is written after (myAge++).
In a simple statement, it doesn't much matter which you use, but in a complex statement, when you are incrementing (or decrementing) a variable and then assigning the result to another variable, it matters very much. The prefix operator is evaluated before the assignment, the postfix is evaluated after.
The semantics of prefix is this: Increment the value and then fetch it. The semantics of postfix is different: Fetch the value and then increment the original.
This can be confusing at first, but if x is an integer whose value is 5 and you write
int a = ++x; you have told the compiler to increment x (making it 6) and then fetch that value and assign it to a. Thus, a is now 6 and x is now 6.
If, after doing this, you write
int b = x++; you have now told the compiler to fetch the value in x (6) and assign it to b, and then go back and increment x. Thus, b is now 6, but x is now 7. Listing below shows the use and implications of both types."
I used this C++ code
The reason is simple. The ++ operator makes a copy of the var, increase the value then return the copied val. Assuming the operators are evaluated left to right it will pass 10-10(then inc C) thus its always 0. However in most cases depending on order is 'undefined behavior' and some compilers like gcc will report a warning (example http://codepad.org/UPBqO38B)
int main()
{
int c=10;
printf("%d", c-c++);
}
精彩评论