← Back to C++ Course | Chapter 2: Input & Output | Lesson 3 of 8

C++ New Lines

endl moves output to a new line and flushes the buffer, while the \n escape sequence inserts just the newline character, making \n the faster choice inside tight loops.
Syntax
cpp
std::cout << "text" << std::endl;

std::cout << "text\n";

The endl Manipulator

endl is a C++ stream manipulator that moves output to a new line and also flushes the output buffer, guaranteeing the text printed so far is actually written out immediately.

Example: The endl Manipulator

cpp
#include <iostream>

int main() {
	std::cout << "Line one" << std::endl; // moves to new line and flushes
	std::cout << "Line two" << std::endl;
	return 0;
}

The \n Escape Sequence

The \n escape sequence, inherited from C, represents a newline character and can be embedded directly inside a string literal to move output to a new line at that exact point.

Example: The \n Escape Sequence

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Line one\nLine two\n";
	// Return 0 to signal that the program finished successfully
	return 0;
}

endl vs \n

Unlike endl, \n only inserts the newline character without forcing a buffer flush, which makes it the faster choice when printing many lines in a tight loop.

Example: endl vs \n

cpp
#include <iostream>

int main() {
	for (int i = 0; i < 3; i++) {
		std::cout << i << "\n"; // faster: no forced flush each iteration
	}
	return 0;
}

Multiple Lines in One Statement

Multiple lines of output can be built either by chaining several \n-terminated strings in one cout statement, or by using separate cout statements each ending in endl.

Example: Multiple Lines in One Statement

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Line one\n" << "Line two\n";
	// Print to the console with cout
	std::cout << "Line three" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Blank Lines

A blank line between two pieces of text is produced by two consecutive newlines, whether written as two chained endl manipulators or two \n characters next to each other in a string.

Example: Blank Lines

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Above" << std::endl << std::endl;
	// Print to the console with cout
	std::cout << "Below" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting endl or \n, so successive outputs run together on one line.
  2. Writing /n instead of \n, which prints a slash and an n rather than a new line.
  3. Using endl everywhere in loops, which also flushes the buffer and can slow output compared with \n.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.