如何使用cout输出\符号?

时间:2009-10-30 16:44:52

标签: c++

如何使用cout输出\符号?

7 个答案:

答案 0 :(得分:22)

使用两个反斜杠\\

答案 1 :(得分:22)

除了所有正确的答案外,请参阅此内容以获取更多转义字符

\a  Bell (beep)
\b  Backspace
\f  Formfeed
\n  Newline
\r  Return
\t  Tab
\\  Backslash
\'  Single quote
\"  Double quote
\xdd    Hexadecimal representation
\ddd    Octal representation
\?  Question mark ('?')

答案 2 :(得分:16)

'\'字符是C和C ++中的转义字符。您可以通过自身转义输出文字'\':

   cout << "This is a backslash: \\";

答案 3 :(得分:4)

也许

cout << "\\";

答案 4 :(得分:4)

std::cout << '\\';

答案 5 :(得分:3)

从C ++ 11开始,您还可以使用raw string literals

std::cout << R"(There is no need to escape backslash here \)";

原始字符串文字中不处理转义字符(如\ n \ t或“”)。

答案 6 :(得分:0)

cout << "\\" << endl;

而不是endl你可以:

cout << "\\ \n";