14

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; invokes the following function of class 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?

Tango Xiao
  • 319
  • 2
  • 11

1 Answers1

15

This is a multicharacter literal and has type 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.

You should use "\\\\", which is char const[3]: two \ and a NUL byte at the end.

Niall
  • 28,969
  • 9
  • 96
  • 135
Simple
  • 13,384
  • 2
  • 41
  • 46