Hi everyone,
This might be a very stupid question to ask and the answer might be very simple but I was just curious to know this.

How to display "\n" on output screen in C++?
We all know \n is used to enter a new line. But what if I want to print \n on screen.
Similarly, how to print ""(double quotes) on the screen?

You put a backslash in front of the special character (the double-quote or the other backslash) in order to remove its specialness and simply display it as is. Try this: cout << "\"\\n\"" << '\n';

Hey....thanks for your help. The code worked but its a bit confusing. Can you give another example?
Suppose, we want the following as output:-

This is an example to print \n and " "

Try cheating. More fun.

cout<<"This is an example to print \\n and "<<(char)34<<(char)32<<(char)34;

The Language Primary Source:

simple-escape-sequence: one of
\' \" \? \\
\a \b \f \n \r \t \v
...
Alphabetic escape sequences representing nongraphic characters in the execution character set are intended to produce actions on display devices as follows:
\a (alert) Produces an audible or visible alert without changing the active position.
\b (backspace) Moves the active position to the previous position on the current line. If the active position is at the initial position of a line, the behavior of the display device is unspecified.
\f (form feed) Moves the active position to the initial position at the start of the next logical page.
\n (new line) Moves the active position to the initial position of the next line.
\r (carriage return) Moves the active position to the initial position of the current line.
\t (horizontal tab) Moves the active position to the next horizontal tabulation position on the current line. If the active position is at or past the last defined horizontal tabulation position, the behavior of the display device is unspecified.
\v (vertical tab) Moves the active position to the initial position of the next vertical tabulation position. If the active position is at or past the last defined vertical tabulation position, the behavior of the display device is unspecified.

Be careful: cout and stdout are not devices so you can't control console cursor movement with all these codes.

This article has been dead for over six months. Start a new discussion instead.