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
  • 3
  • 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
  • 30,036
  • 10
  • 99
  • 142
Simple
  • 13,992
  • 2
  • 47
  • 47