2011-04-26 24 views
3
#include <iostream> 

int main() { 
int x = 1; 
for(int i = 0; i < 100; ++i); 
    // What will the next line do? Increment???????????/ 
    ++x; 
std::cout << x << std::endl; 
} 

爲什麼代碼打印1而不是2?C++正在吹我的腦海

+0

它爲我打印2。 – 2011-04-26 16:08:22

+0

它沒有。它打印'2'。 – 2011-04-26 16:08:24

+1

打印1給我 – 2011-04-26 16:12:12

回答

34

trigraph sequence??/序列轉換爲'\'所以++x是註釋行的一部分。

這個gotw article討論了同樣的問題[完全相同的代碼]。

+6

胡,男人,三撇子再次擊中。 – 2011-04-26 16:09:42

+0

爲了完整起見,我認爲這是第一次出現在這裏:http://www.gotw.ca/gotw/086.htm – RedX 2011-04-26 16:10:20

+1

前一段時間不是trigraphs過時了嗎?至少在gcc中?無論如何+1對於一個敏銳的眼睛;-) – hirschhornsalz 2011-04-26 16:10:51

相關問題