Skip to content
C++ Better Explained
Go back
C++ endl vs \n: What's the Difference and Which to Use
Edit page

C++ endl vs \n: What’s the Difference

Almost every C++ program prints output, and you’ll quickly meet two ways to end a line: std::endl and the \n character. They look interchangeable, and usually produce the same visible result — but there’s an important difference under the hood that affects performance.


They Both Start a New Line

First, the obvious part. Both of these print “Hello” and “World” on separate lines:

#include <iostream>

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

The output is identical:

Hello
World

So far they seem the same. The difference is what happens besides the newline.


The Hidden Difference: Flushing

Output in C++ doesn’t always go to the screen instantly. For efficiency, it’s collected in a buffer and written out in batches. std::endl does two things: it inserts a newline and flushes the buffer, forcing everything written so far to appear immediately. The \n character only inserts the newline — it leaves flushing to happen naturally later.

std::cout << "Loading" << std::endl;  // newline + flush (appears now)
std::cout << "Loading" << "\n";        // newline only (flush happens later)

Most of the time you never notice, because the buffer gets flushed automatically when the program ends or when input is requested.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Why It Matters for Performance

Flushing is a relatively expensive operation. If you do it on every single line inside a big loop, the cost adds up:

// Slower: flushes 100,000 times
for (int i = 0; i < 100000; i++) {
    std::cout << i << std::endl;
}

// Faster: flushes only once at the end
for (int i = 0; i < 100000; i++) {
    std::cout << i << "\n";
}

The second loop can be several times faster on large outputs because it isn’t forcing a flush on every iteration. This is why experienced C++ programmers default to \n for ordinary printing.


When endl Is Actually Useful

The flush isn’t always wasted. There are times you genuinely want the text on screen right now:

When debugging a crash, a flushed endl guarantees your last message appears before the program dies — an unflushed buffer might be lost. Similarly, if you print a progress message just before a long computation, endl ensures the user sees it immediately rather than after the work finishes. In interactive programs where timing of output matters, endl gives you control.


The Practical Rule

Use \n by default — it’s faster and does everything you normally need. Reach for std::endl only when you specifically want an immediate flush, such as debugging output or progress messages before slow operations. If you want a newline and a manual flush occasionally, you can also write std::cout << "\n" << std::flush; to make the intent explicit.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Next Post
C++ Factorial Program: Loop and Recursion Methods Explained