Skip to content

What Is the Difference Between endl and Backslash n in C++

In this guide
  1. What each one does
  2. What a flush is
  3. Three ways to write a newline
  4. When flushing matters
  5. A simple rule for beginners

Short answer

Both std::endl and the newline character '\n' move the cursor to the next line. The difference is that std::endl does a second job, it flushes the output buffer, which is often slower. Learn C++ says to prefer '\n' when printing text to the console, and that is the habit worth building from day one.

What each one does

Learn C++ describes std::endl as standing for "end line". It outputs a newline, but it "actually does two jobs", according to the same lesson: it moves to the next line and it flushes. The newline character '\n', by contrast, "moves the cursor to the next line of the console without causing a flush, so it will typically perform better."

Here is a small program using both, compiled and run with g++ 16.2 on Windows:

#include <iostream>

int main()
{
    std::cout << "Hi!" << std::endl;
    std::cout << "My name is Alex.\n";
    std::cout << "Done" << '\n';
    return 0;
}

It printed these three lines:

Hi!
My name is Alex.
Done

On the screen the result looks identical whichever you choose. The difference is invisible in a small program, which is why beginners often never notice it.

What a flush is

Learn C++ explains that output written with std::cout is "typically not sent to the console immediately". It is collected in a buffer, and periodically the buffer is flushed, meaning all the data collected is transferred to its destination. It adds that buffering "can significantly increase performance by batching multiple output requests together". So flushing after every line, which is what std::endl does, throws away that benefit.

Three ways to write a newline

The lesson shows that '\n' works in three places:

StyleExampleLearn C++ says
Single quoted, by itselfstd::cout << '\n';Conventional
Double quoted, by itselfstd::cout << "\n";Unconventional but okay
Inside existing textstd::cout << "That is all\n";Conventional

Use single quotes when the newline stands alone, and put it inside the string when it comes at the end of a line of text.

When flushing matters

You do not need to flush by hand in a beginner program. cppreference notes one case that happens for you: std::cin is tied to std::cout, so any input operation on std::cin forces a flush of pending output. That is why a prompt such as "Enter a number: " appears before the program waits for you, even with no newline after it. This program was compiled and run with 42 as the input:

#include <iostream>

int main()
{
    std::cout << "Enter a number: ";
    int x{};
    std::cin >> x;
    std::cout << "You entered " << x << '\n';
    return 0;
}

The output was Enter a number: You entered 42. The prompt and the answer share one line here because the input was piped in rather than typed, so no Enter key echoed a new line.

A simple rule for beginners

  1. End lines of output with '\n'.
  2. Do not reach for std::endl unless you have a reason to flush.
  3. Do not worry about the difference when a program prints a few lines.

If you are not sure what the lines around these statements mean, start with what int main() means in C++ and what using namespace std means. For the bigger picture, see how to learn C++ as a beginner.

C++ Game: Segfault

Teaches C++ to complete beginners as a game where you tap one real line of code at a time.

Get it on Google Play

Free to play. Halls 1 to 4 are free with no time limit, and free players can watch optional rewarded video ads for bonuses. Segfault Pro, a monthly subscription or a 3-month pass paid once, opens hall 5 and up, unlimited practice sets and mock exams, with no ads.

C++ Game: Segfault teaches C++ to complete beginners as a game. You never type. Each card asks you to tap the one real line of C++ that makes a box match the value it needs, and the line you ran appears underneath. It covers 11 halls, from variables to if, loops, functions and arrays, and halls 1 to 4 are free. Segfault will not make you a programmer on its own, because you tap lines rather than write them.

Sources

  1. 1.5 Introduction to iostream cout, cin, and endl - Learn C++ (learncpp.com)
  2. std::cin, std::wcin - cppreference.com

Search the site