简体   繁体   English

为什么声明“cout <<'\\\\\\\\';”没有失败?

[英]Why does the statement “cout << '\\\\';” not fail?

The source code is as the following. 源代码如下。

cout << '\\' << endl;  //OK, output is \  
cout << '\\\\' << endl;  //OK, output is an integer 23644, but why? 

The statement cout << '\\\\\\\\' << endl; 声明cout << '\\\\\\\\' << endl; invokes the following function of class ostream . 调用类ostream的以下函数。

_Myt& __CLR_OR_THIS_CALL operator<<(int _Val)

I know it is strange to write the expression '\\\\\\\\' , But I don't understand why it doesn't fail. 我知道写'\\\\\\\\'这个词很奇怪,但我不明白为什么它不会失败。 How to explain the result? 如何解释结果?

This is a multicharacter literal and has type int . 这是一个多字符文字,类型为int

[lex.ccon]/2 : [lex.ccon] / 2

An ordinary character literal that contains more than one c-char is a multicharacter literal. 包含多个c-char的普通字符文字是多字符文字。 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. 包含单个c-char的多字符文字或普通字符文字在执行字符集中无法表示,它是有条件支持的,具有int类型,并具有实现定义的值。

You should use "\\\\\\\\" , which is char const[3] : two \\ and a NUL byte at the end. 你应该使用"\\\\\\\\" ,这是char const[3] :两个\\和最后一个NUL字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM