c++中的流提取操作符

Stream extraction operator in C++

本文关键字:提取 操作符 c++      更新时间:2023-10-16

我有以下代码:

#include<iostream>
using namespace std;
 int main() 
{
 int x=3;
 cout<<x++<<++x<<x++<<++x<<endl;
return 0; 
}

输出应为3557但它是6747。为什么? ?

:

#include<iostream>
using namespace std;
 int main() 
{
 int x=3;
    cout <<x++<<endl;
    cout<<++x<< endl;
    cout<<x++<< endl;
    cout<<++x<< endl;
return 0; 
}

以上代码给出:3.557(每一个数字换行)有人能解释一下原因吗?

 int x=3;
 cout<<x++<<++x<<x++<<++x<<endl;

这是未定义的行为,因此您可以轻松地获得任何结果。

int x=3;
cout <<x++<<endl; // x = 3, post incremented to 4...
cout<<++x<< endl; // here x = 4 and preincremented to x = 5
cout<<x++<< endl; // x = 5 , postincremented to 6...
cout<<++x<< endl; // so here x = 6 but preincremented to 7

未定义的行为和序列点

cout和printf