C++: “std::endl” vs “\n”

0 votes
asked Oct 17, 2008 by head-geek

Many C++ books contain example code like this...

std::cout << "Test line" << std::endl;

...so I've always done that too. But I've seen a lot of code from working developers like this instead:

std::cout << "Test line\n";

Is there a technical reason to prefer one over the other, or is it just a matter of coding style?

9 Answers

0 votes
answered Oct 17, 2008 by martin-beckett

There might be performance issues, std::endl forces a flush of the output stream.

0 votes
answered Oct 17, 2008 by ferruccio

They will both write the appropriate end-of-line character(s). In addition to that endl will cause the buffer to be committed. You usually don't want to use endl when doing file I/O because the unnecessary commits can impact performance.

0 votes
answered Oct 17, 2008 by zee-jollyroger

I've always had a habit of just using std::endl because it is easy for me to see.

0 votes
answered Oct 17, 2008 by david-thornley

The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for.

The only difference is that std::endl flushes the output buffer, and '\n' doesn't. If you don't want the buffer flushed frequently, use '\n'. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl.

0 votes
answered Oct 17, 2008 by loki-astari

The difference can be illustrated by the following:

std::cout << std::endl;

is equivalent to

std::cout << '\n' << std::flush;

So,

  • Use std::endl If you want to force an immediate flush to the output.
  • Use \n if you are worried about performance (which is probably not the case if you are using the << operator).

I use \n on most lines.
Then use std::endl at the end of a paragraph (but that is just a habit and not usually necessary).

Contrary to other claims, the \n character is mapped to the correct platform end of line sequence only if the stream is going to a file (std::cin and std::cout being special but still files (or file-like)).

0 votes
answered Oct 17, 2009 by nathan

There's another function call implied in there if you're going to use std::endl

a) std::cout << "Hello\n";
b) std::cout << "Hello" << std::endl;

a) calls operator << once.
b) calls operator << twice.

0 votes
answered Oct 17, 2010 by smerlin

If you use Qt and endl, you could accidentally use the wrong endl, happened to me today and i was like ..WTF ??

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui>
//notice that i dont have a "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::cout << "Finished Execution !" << endl << "...";
    // Line above printed: "Finished Execution !67006AB4..."
    return qapp.exec();
}

Of course that was my mistake, since i should have written std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.*

Of course you could recompile Qt to use a namespace, so you get a compilation error for the example above.

EDIT: Forgot to mention, Qt's endl is declared in "qtextstream.h" which is part of QtCore

*EDIT2: C++ will pick the correct endl if you have a using for std::cout or the namespace std, since std::endl is in the same namespace as std::cout, C++'s ADL mechanism will pick std::endl.

0 votes
answered Oct 29, 2014 by emily-l

I recalled reading about this in the standard, so here goes:

See C11 standard which defines how the standard streams behave, as C++ programs interface the CRT, the C11 standard should govern the flushing policy here.

ISO/IEC 9899:201x

7.21.3 §7

At program startup, three text streams are predefined and need not be opened explicitly — standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.

7.21.3 §3

When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.

This means that std::cout and std::cin are fully buffered if and only if they are referring to a non-interactive device. In other words, if stdout is attached to a terminal then there is no difference in behavior.

However, if std::cout.sync_with_stdio(false) is called, then '\n' will not cause a flush even to interactive devices. Otherwise '\n' is equivalent to std::endl unless piping to files: c++ ref on std::endl.

0 votes
answered Oct 22, 2015 by gentle

If you didn't notice, endl is like pressing the ENTER KEY while "\n" is like pressing ENTER KEY + SPACE BAR.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...