Endl vs \n in C++
At first glance, std::endl and \n may seem to have the same effect in C++. Both insert a new line character into the output stream. However, there is a subtle difference between the two: std::endl inserts a new line character and flushes the output stream, while \n only inserts a new line character.
Flushing the output stream means that any data that has been buffered up for output is immediately written to the output device. This can be important in some cases, such as when you want to make sure that output is displayed immediately. For example, if you are writing a progress indicator or a status message, you will want to use std::endl to ensure that the output is displayed as soon as possible.
The following figure shows scenario when endl is used :

For additional information about endl, please read: What Does Endl Mean in C++?
Differences between endl and \n
| Feature | endl | \n |
|---|---|---|
| Type | Output manipulator | Character |
| Memory representation | Inserts a newline character and flushes the output buffer | Inserts a newline character only |
| Memory usage | 0 bytes (no additional memory required) | 1 byte (occupies space in memory) |
| Interpretation in strings | Treated as a keyword and does not represent a literal newline | Interpreted as a literal newline character |
| Placement in strings | Cannot be directly inserted within double quotes | Can be inserted within double quotes |
| Language support | Exclusive to C++ | Supported in both C and C++ |
| Output buffer behavior | Flushes the output buffer, ensuring immediate output to the console | Does not flush the output buffer; output is buffered until the buffer is full or the program terminates |
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Examples
some examples of the differences between endl and \n in C++:
Example 1:
Flushing the output buffer with endl
In this example, the output of the program is:
This is because std::endl flushes the output buffer after each line of output.
Flushing the output buffer with \n
In this example, the output of the program is:
This is because '\n' does not flush the output buffer. Instead, the output is buffered until the program terminates or the buffer is full.
Turn Learning into Career Growth
Example 2:
Using endl in C++ and \n in C
This code will compile and run in C++, but the above code will not work in C because endl was introduced in C++
This code will compile and run in C as well as in C++ also.
Example 3: Memory usage of endl and \n
The output of this program is:
This is because std::endl is an output manipulator, a function that modifies the output stream. Output manipulators do not occupy any additional memory. However, '\n' is a character occupying 1 byte of memory.
Conclusion
-
In this article, we learned the difference between endl and \n in C++.
-
Both endl and \n are used for inserting a new line in C++.
-
The main difference is the way each of them interacts with the output buffer.
-
In small programs, there is a negligible difference between endl and \n, whereas for large programs including multiple IO Operations, endl in C++ introduces the extra overhead of flushing the buffer.