Can someone please explain what \n\n means in a line of text in C++? For example ("\n\nWelcome to Main Menu\n\n");

Thanks

\n means new line

Start:


Welcome to main menu


End.

Can someone please explain what \n\n means in a line of text in C++? For example ("\n\nWelcome to Main Menu\n\n");

Thanks

A \n character is a special type of character(new line character), termed as an escape sequence. It serves the same purpose as endl , which is to move the console cursor down by one line.

For more information on escape sequences, look here.

>It serves the same purpose as endl

Not likely. There is a difference between std::endl and \n.
(To the OP: The following explanation may not suited for you. The answer of your question was well given in the posts above)
To understand lets see what happens you do a cout.
When you issue a std::cout statement, the data you passed is stored in the stream buffer and is displayed only when there is appropriate time and space to do so. Displaying the stream buffer on to the standard output is proceeded flushing the buffer stream(that is to empty the stream). Once the data has been sent to the standard output, the stream is automatically flushed. Alternatively, you can force a stream flush by using the .flush() member function.

'\n' is a escape sequence but std::endl is a stream manipulator. When you issue a std::endl, it does two jobs

  • Prints a new line and
  • Forces the stream to standard output and flushes the stream.

Hence, when you use std::endl, you are sure that it would print the result to the output device immediately.

commented: Great Info... :) +1
commented: Right! +6
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.