Difference Between std::endl and \n in C++



In C++, both std::endl and \n are used for inserting a newline in the output stream. However, std::endl also clears the output buffer by sending all the stored output to the screen.

In this article, we will see a detailed comparison along with a table and discuss the scenarios where each should be used in C++.

\n (newline character)

'\n' is an escape sequence that represents a newline character, which makes the cursor move to the next line. This is also used inside string literals to create line breaks. It's faster compared to endl but doesn't flush.

When to use '\n'

  • When you need a newline without flushing the output buffer.
  • In loops where performance matters.
  • In input/output file operations.

Example

#include <iostream>
using namespace std;
int main() {
    int a = 5;
    cout << "The value of a is: " << a << "\n";
    cout << "Hello,TutorialsPoint Learner!\n";
    cout << "Welcome to \nC++ programming.\n";
    return 0;
}

Output

The value of a is: 5
Hello,TutorialsPoint Learner!
Welcome to 
C++ programming.

std::endl

The endl is a stream manipulator in C++, which is used to output stream (cout),

Here, two things happen simultaneously.

  • It inserts a newline character (making it move to the next line).
  • Flushes the output buffer (which means it forces all pending output to display on the screen immediately).

So it's like a combination of '\n' + flush. Therefore, compared to '\n', its performance is slower because of flushing.

When to use std::endl

  • This is used when you need both: a newline and immediate output (like in debugging or logging).
  • This is useful when working with real-time systems or where flushing is necessary before input.

Example

#include <iostream>
using namespace std;

int main() {
    cout << "Hello! Learner"<<endl;
    cout<< "Welcome to"<<endl<<"TutorialsPoint";
    return 0;
}

Output

Hello! Learner
Welcome to
TutorialsPoint

Example Code

Here is the following code example showcasing the behavior of std::endl and \n, along with the flush effect for std::endl.

#include <iostream>
#include <thread>
#include <chrono>

using namespace std; 

int main() {
    
    // Using std::endl (flushes immediately)
    cout << "Using std::endl - This will show immediately." << endl;
    this_thread::sleep_for(chrono::seconds(3)); // Delay for 3 seconds
    
    // Using \n (no flush)
    cout << "Using \n - This might not show immediately." << endl;
    this_thread::sleep_for(chrono::seconds(3)); // Delay for 3 seconds

    cout << "Program completed!" << endl;

    return 0;
}

Output

Using std::endl - This will show immediately.
Using \n - This might not show immediately.
Program completed!

Comparison Table

Aspect \n (Newline Character) std::endl
Function Adds a newline Adds a newline and flushes
Performance Faster Slower (due to flush)
Memory Usage Minimal Slightly more memory usage due to flushing
Use in Loops Efficient Should be avoided in high-frequency loops
Updated on: 2025-05-02T19:20:57+05:30

397 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements