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 " "

Hey understood the concept. Thanks a lot for your help.

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.

You're probably 8 years late, @pankaj_13. Even tho it's an old question, I'm going to suggest one way of displaying \n on the console, using C++11:

Using raw strings (string literal officialy):

#include <iostream>

int main()
{
    std::cout << R"(\n)";
    return 0;
}

You simply type your string, enclose it between parentheses and then brackets, finally placing a 'R' (from raw) behind all of it.

In any case, DO NOT use '\n' for new lines in C++. They generally will not flush the output buffer. Use the 'endl' operator instead, as in: cout << "This is a complete line." << endl;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.