源代码如下。
cout << '\\' << endl; //OK, output is \
cout << '\\\\' << endl; //OK, output is an integer 23644, but why?
声明cout << '\\\\' << endl;
调用类 ostream
的以下函数.
_Myt& __CLR_OR_THIS_CALL operator<<(int _Val)
我知道写'\\\\'
这个表达式很奇怪, 但我不明白为什么它不会失败。结果如何解释?
最佳答案
这是一个多字 rune 字,类型为 int
。
An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal, or an ordinary character literal containing a single c-char not representable in the execution character set, is conditionally-supported, has type int, and has an implementation-defined value.
你应该使用"\\\\"
,也就是char const[3]
:两个\和最后一个NUL字节。
关于c++ - 为什么语句 “cout << '\\\\';” 不会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36496013/