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.
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.
Related Articles
- C++ Hello World Explained — your first cout statement
- C++ cin User Input — reading input from the keyboard
- C++ iomanip Formatting — controlling how output looks
- C++ stringstream — building strings from streams
- C++ String Handling — working with text
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.