Endl vs \n in C++

Video Tutorial
FREE
Input Output in C++ thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

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 not only inserts a new line character but also 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 :

endl is inserted in cpp

For additional information about endl, please read: What Does Endl Mean in C++?

Differences between endl and \n

Featureendl\n
TypeOutput manipulatorCharacter
Memory representationInserts a newline character and flushes the output bufferInserts a newline character only
Memory usage0 bytes (no additional memory required)1 byte (occupies space in memory)
Interpretation in stringsTreated as a keyword and does not represent a literal newlineInterpreted as a literal newline character
Placement in stringsCannot be directly inserted within double quotesCan be inserted within double quotes
Language supportExclusive to C++Supported in both C and C++
Output buffer behaviorFlushes the output buffer, ensuring immediate output to the consoleDoes 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, which is a type of function that modifies the output stream. Output manipulators do not occupy any additional memory. However, '\n' is a character, which does occupy 1 byte of memory.

Conclusion

  • In this article, we learned about 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.