据说是一道面试题...



lrb2010
2014-03-18 16:06:55

[code]#include

using namespace std;

int main(void){
int i = 100;
i += i++;
cout<<"i += i++;"<
i = 100;
i += ++i;
cout<<"i += ++i;"<
i = 100;
++i += i++;
cout<<"++i += i++;"<
i = 100;
++i += ++i;
cout<<"++i += ++i;"<}
[/code]求程序的输出结果...


ProfaneAria
2014-03-18 21:27:23

[i=s] 本帖最后由 ProfaneAria 于 2014-3-18 21:37 编辑 [/i]

201,202,203,204.。。看完就出来了。。
i += i++; 具体过程为 (100 + 100) + 1 = 201
i += ++i; 具体过程为 (100 + 1) + (100 + 1) = 202
++i += i++; 具体过程为 (100 + 1) + (100 + 1) + 1 = 203
++i += ++i; 具体过程为 (100 + 1 + 1) + (100 + 1 + 1) = 204
原因是优先级 ++i > += > i++
我是有多无聊。。。。