c++ - 为什么语句 “cout << '\\\\';” 不会失败?

标签 c++ string visual-c++ char cout

源代码如下。

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

[lex.ccon]/2 :

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/

相关文章:

c++ - 请解释这种奇怪的行为 c++ 乘以数字然后 mod

c++ - 用模板复制构造函数替换默认复制构造函数

string - 每 n 个字符拆分字符串

c++ - 删除 std::string 上的反斜杠字符时出现问题

visual-c++ - Visual Studio 2013 创建更大的 exe - 没有 MFC

c++ - 如何在 Visual C++ 中对 x64 项目运行代码分析?

c++ - MFC CEdit 将非ascii字符转换为ascii

c++ - Windows 64 位结构大小随包含的数据类型而变化?

c++ - 将文件读入Qt

java - 查找由其他字符串的字符组成的最长子字符串