Why are escape characters not working when I read

2020-06-30 08:40发布

(a)

string str = "Hello\nWorld";

When I print str, the output is:

Hello
World

(b)

string str;
cin >> str;      //given input as Hello\nWorld

When I print str, the output is:

Hello\nWorld

What is the difference between (a) and (b)?

标签: c++
7条回答
乱世女痞
2楼-- · 2020-06-30 09:34

cin does not include a C++ compiler. Escape sequences in string literals are a feature of C++'s lexer, which is part of the C++ compiler. Streams more or less give you what came from the OS (they may do some CRLF -> CR translation or similar based on the OS, but that's it).

查看更多
登录 后发表回答