Table of Contents
The newline character ('\n') is used to move the cursor to the beginning of the next line. It is the most commonly used method for creating line breaks in C++ output because it does not automatically flush the output stream.
Unlike std::endl, the newline character simply inserts a line break and allows the output stream to continue buffering data. This generally provides better performance, especially when producing large amounts of output.
Source Code #
#include <iostream>
int main()
{
std::cout << "First Line\n";
std::cout << "Second Line\n";
std::cout << "Third Line\n";
return 0;
}
Output #
First Line
Second Line
Third Line
Explanation #
'\n'is the newline escape character used to move the cursor to the next line.- The newline character can be included directly inside a string literal or inserted separately into the output stream.
- Unlike
std::endl,'\n'does not flush the output stream after printing the newline. - Since the output buffer is not flushed automatically, using
'\n'is generally more efficient for normal output operations. - The newline character is suitable for most console programs where immediate output is not required.
std::coutwrites the text to the standard output stream.return 0;terminates the program successfully.