2013-10-22 53 views
0

我检查都是由编译器:为什么输出不一样?

的这个输出是10

int count = 0; 
    for(int i=0; i < 10; ++i){ 
      count=++count; 
    } 
    cout << count; 

我不知道为什么这样的输出(++计数变成计数++)是0

int count = 0; 
    for(int i=0; i < 10; ++i){ 
      count=count++; 
    } 
    cout << count; 
+6

未定义行为。 – 0x499602D2

回答

4

随着

 count=++count; 

 count=count++; 

这两个程序都会遇到未定义的行为,因为您在修改count时没有插入顺序点。请注意,=运算符不会引入序列点。

强制性阅读UB ;-)

Undefined Behavior and Sequence Points in C++

相关问题