Xcode 和 VisualC++输出流的差别的理解

将这样一段程序分别运行与Visual Studio 和 Xcode上边的结果:

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a=0, b=0, c=2,d=0,e=2,f=2;
 6     cout<<a<<" "<<a++<<endl;
 7     cout<<++b<<" "<<b++<<" "<<endl;
 8     cout<<c<<" "<<(c++)+(++c)<<" "<<endl;
 9     cout<<(d=f++)+(e=f)<<endl;
10     cout<<f<<" "<<d<<" "<<e<<endl;
11     return 0;
12 }

Visual C++运行下:

1 0       //从右向左,先打印a(0),在打印自加后的a(1);

2 0       //从右向左,先打印b++(0),再打印(++b)(2);

4 6       //从右向左,表达式中如果有左++,先自增,所以(c++)(3)与++c(3)的和是6;再打印左边c(4);

4          //a=f++的值为2,visualC++中先算完加号,再统一计算f++,所以e=f表达式的值为2,两者相加结果为4;

3 2 2    //作为上一个表达式的副作用,f=3;另外d=2 e=2;

Xcode编译运行下:

0 0      //从左向右,

1 1      //从左向右

2 6      //可能采用了跟VC++一样的机制

5

3 2 3

devC++编译运行下:

晚来一阵风兼雨
原文地址:https://www.cnblogs.com/dejunwang/p/4755586.html