"C-C ++";关于后缀/前缀操作顺序的玩笑 [英] "C - C++" joke about postfix/prefix operation ordering
问题描述
我的朋友给我开了个玩笑:
My friend sent me a joke:
问C和C ++有什么区别?
Q. What's the difference between C and C++?
A.没什么,因为:(C-C ++ == 0)
A. Nothing, because: (C - C++ == 0)
我试图更改订单并被卡住.
I tried to change order and got stuck.
看下面的代码:
public class Test {
public static void main(String args[]) {
int c = 10;
System.out.println(c++ - c);
System.out.println(++c - c);
}
}
为什么返回:
-1
0
我了解后缀和前缀增量.为什么这不是结果?
I understand postfix and prefix increment. Why isn't this the result?
0
1
推荐答案
因为在第一个示例中,c
始于10.c++
递增c
并返回10,因此第二个c
现在求值为11,因为它已递增.因此计算出的最终表达式是10 - 11
,等于-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.
在第二个示例中,++c
再次递增c
,但由于它是预递增的,所以返回12.第二个c
的计算结果也为12,因为它是存储在c
中的新值.因此该表达式最终被评估为12 - 12
,等于0.
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.
这篇关于"C-C ++";关于后缀/前缀操作顺序的玩笑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!