与之相关的区别 Operator Priority使用前缀和后缀
// program 01
int x =10;
int c = x++ + ++x;
Console.WriteLine(c); // output will be 22
// program 02
int x =10;
int c = ++x + x++;
Console.WriteLine(c); // output will be 22
// program 03
int x =10;
int c = ++x + x;
Console.WriteLine(c); // output will be 22
答案 0 :(得分:7)
以下是评估这些表达式的方法:
1:
x++ + ++x;
| | |
1 3 2
1: increments x to 11, returns value before increment (10)
2: increments x to 12, returns value after increment (12)
3: 10 + 12 = 22
2:
++x + x++;
| | |
1 3 2
1: increments x to 11, returns value after increment (11)
2: increments x to 12, returns value before increment (11)
3: 11 + 11 = 22
3:
++x + x
| | |
1 3 2
1: increments x to 11, returns value after increment (11)
2: returns value of x (11)
3: 11 + 11 = 22
值得注意的是,某些C / C ++实现可能会以不同的顺序评估此表达式。此页面包含与OP相同的未定义行为的示例。 http://en.cppreference.com/w/cpp/language/eval_order