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 |
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.
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.