I don't have a specific context in which I'm asking the question, but while I was reading a beginner book on C++ I noticed the use of both an endl stream manipulator and a newline escape character when dealing with a stream object.

The exmaple is as follows:

cout << "Hello World" << endl;
cout << "Hello World\n";

My questions are:

  1. Is it more appropriate to use the stream manipulator (endl) in a certain situation and an escape character in a different one?
  2. Are there drawbacks efficiency wise to using one of the two?
  3. Are they completely interchangeable?
  4. I read that an escape sequence is stored in memory as a single character. Does that mean it is more appropriate to use endl if you're going for low memory consumption?
  5. Does the stream manipulator endl use up memory in any way, if so is it more than the escape sequence?

Thanks, StackExchange Apologies if I posted this in the wrong section, I thought it counted as data structures.

  • 3
    endl also causes a flush on some streams, '\n' does not.
    – James
    Jan 28, 2013 at 15:39

2 Answers 2


o << std::endl is equivalent to the following code:


In other words, you should only use std::endl when you need to flush the stream. For example:

  • You're about to perform a time-consuming operating and want to make sure you display a message before doing so.
  • Another process is waiting on your output.
  • If more than one thread or process is running, you may need to flush output so that the output from each thread or process is displayed correctly. (Otherwise, you may get seemingly random blocks of output interleaved at awkward intervals.)

If you don't need to flush the stream, then use \n instead of std::endl. The extra calls to flush can hurt performance (sometimes significantly).

For more details, see cppreference.com.

Regarding memory usage: Worrying about \n versus std::endl is almost certainly an unnecessary micro-optimization, but in general, I'd expect \n to take less memory. \n is just one more byte at the end of a string literal, while writing std::endl is translated by the compiler into (probably inlined) function calls to put and flush.

Platform-specific differences in line endings (Windows \r\n versus Linux and OS X \n) are handled at a lower level than std::endl and \n:

  • If a stream is opened in text mode, then if you write \n, it automatically translates it to the appropriate platform-specific line ending. If you read a platform-specific line ending, the stream automatically translates it to \n.
  • If a stream is opened in binary mode, then they pass any line endings through verbatim, and it's up to you.
  • For std::cout and std::cin in particular, they're treated as if they're text mode.

1) For portability, use endl. Windows newlines are \r\n, Linux \n and Mac \r. Edit: As per comments, line system specific endings are handled at a lower level.

2) endl flushes the stream, "\n" does not.

3) Depends on portability.

As to memory usage, you can minimize it by flushing to other storage as often as possible with endl. However, it'll decrease performance.

Edit: Clear up some mistakes.

  • 2
    Mac has been unix based since OS X and thus is also \n on any modern machine.
    – user40980
    Jan 28, 2013 at 15:50
  • 7
    Sorry, but this is incorrect. endl and \n are equivalent with regards to platform-specific line endings; platform differences are handled at a lower level. Jan 28, 2013 at 16:19
  • 2
    Your first point is wrong. endl() sends '\n' to the stream (in addition to flushing it). The '\n' character is converted to the platform specific End of line sequence (assuming text mode). The End of line sequence is converted back to '\n` when read from a file. Point 3) is dubious. And the last paragraph is again wrong: Flushing will not save you space and flushing more than required will slow your code (the point of the buffer is to improve write efficiency to a slow device). Jan 29, 2013 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.