Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; 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?

like image 352
Tango Xiao Avatar asked Apr 08 '16 09:04

Tango Xiao


People also ask

Why is my cout not working?

This may happen because std::cout is writing to output buffer which is waiting to be flushed. If no flushing occurs nothing will print. So you may have to flush the buffer manually by doing the following: std::cout.

What does the cout << statement do?

The C++ cout statement is the instance of the ostream class. It is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

What does cout << do in C++?

The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device i.e. monitor. It is associated with the standard C output stream stdout.

Why is my C++ program not showing output?

Why this C++ code not showing any output? There is not much outputs in this code. You can add some check point outputs (some outputs at strategic points in code to get a print every time execution cross such a point) to get an idea of what your code is doing, or turn to debugger.


1 Answers

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.

like image 66
Simple Avatar answered Sep 28 '22 06:09

Simple