Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个字符数组char str[10]。
char str[10]
如果我将值存储xyz\nabc在strusingchar str[10]="xyz\nabc";并输出它,它会给出以下输出:
xyz\nabc
str
char str[10]="xyz\nabc";
xyz abc
另一方面,如果我从stdin表单中给它输入xyz\nabc然后打印它,它会给出以下输出:
stdin
为什么会这样?
当您从 中读取它时stdin,您会得到准确的字符串"xyz\nabc",就像它出现的那样。
"xyz\nabc"
如果您在代码中对该值进行硬编码(即char* x = "xyz\nabc"),则\n是单个字符,它代表一个新行。要获得相同的输出,您需要char* x = "xyz\\nabc"- 额外的\转义\.
char* x = "xyz\nabc"
\n
char* x = "xyz\\nabc"
\
请参阅此http://en.cppreference.com/w/cpp/language/escape