【发布时间】:2021-03-20 15:18:18
【问题描述】:
我无法理解 Post Increment (++) 和 Pre Increment 在示例中如何协同工作。
x++ 表示变量加 1 但我对这个例子感到困惑:
using namespace std;
/ run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
int a;
a=8;
cout<<++a<<a++<<endl;
cout<<a<<endl;
return 0;
}
我假设这意味着首先增加 1,然后它将首先分配然后增加这意味着结果应该是 9 8 和 9 但是当我编译它时,我得到 10 8 和 10。我不明白.
【问题讨论】:
-
你能看到像
g(f(++a), a++)这样的函数调用的问题吗? -
如何将 8 递增两次得到 9?
-
如果 a = 8:
++a=> 使用 9 仍然是 9a++=> 使用 8 并且仍然是 9
标签: c++ operators post-increment pre-increment