2011-05-05 42 views
0

以下代碼的輸出是什麼?爲什麼此代碼輸出文字「 n」而不是換行符?

std::cout<<"what is the output \\n hello \'world\' world"; 

我想輸出應該是:

what is the output 
hello 'world' world 

但實際產量the output \n hello 'world' world

爲什麼不\n輸出作爲新行?

+6

我們會從你那裏得到很多這些嗎? – 2011-05-05 21:18:17

+0

@unapersson你的意思是? – cong 2011-05-05 21:19:37

+0

因爲\\會輸出一個\。你可以使用'std :: endl'獲得你想要的效果:'std :: cout <<「輸出是什麼」<< std :: endl <<「hello \'world \'world」;' – sitnik 2011-05-05 21:19:53

回答

10

你的雙反斜槓\\是一個逃生,產生\所以你看到\n。如果你想換行,請使用單個反斜槓\n

4

\n指定一個新的行字符。但是如果你想要一個反斜槓字符,會發生什麼?爲此,C++允許您使用\\。第一個反斜槓轉義第二個導致一個反斜槓,沒有特殊的翻譯。

這就是你在這裏,然後n

相關問題