开发者

Why does this code output a literal "\n" instead of a newline?

开发者 https://www.devze.com 2023-03-03 12:13 出处:网络
What is the output of this following code? std::cout<<\"what is the output \\\\n hello \\\'world\\\' world\";

What is the output of this following code?

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

I think the output should be:

what is the output
hello 'worl开发者_开发知识库d' world

But the actual output is the output \n hello 'world' world

Why isn't \n output as a new line?


Your double backslash \\ is an escape that produces \ so you see \n. If you want a newline, use a single backslash \n.


\n specifies a new line character. But what happens if you want a backslash character? For this, C++ allows you to use \\. The first backslash escapes the second resulting in a single backslash and no special translation.

That's what you have here, followed by n.

0

精彩评论

暂无评论...
验证码 换一张
取 消