views:

6399

answers:

9

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?

+5  A: 

Good explanation:

http://cppkid.wordpress.com/2008/08/27/why-i-prefer-n-to-stdendl/

+15  A: 

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

Martin Beckett
And it can do any other processing that the local system requires to make this work well.
dmckee
+8  A: 

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.

Ferruccio
+3  A: 

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

Zee JollyRoger
+42  A: 

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.

David Thornley
Or consider using `::std::cerr` instead of `::std::cout` since it's unbuffered and flushed with each and every output operation.
Omnifarious
@Omnifarious: No std::cerr should be reserved for errors. The two streams are not synced together so if you output some text to cout it may be buffered and the cerr will go direct to the output this resulting in a mixed mode display. Use cerr for what it is supposed to be for (errors) and cout for what it is designed for (normal interaction).
Martin York
@Martin York, I agree, that's what `::std::cerr` is for. But, in my opinion, if you have code where you don't want buffering it's much more likely that your output belongs in `::std::cerr`. I can't think of any good reason to routinely forgo buffering for `::std::cout`.
Omnifarious
std::endl is platform aware too , No?
Lucas
@Lucas: No more than '\n' is platform aware.
Charles Bailey
+15  A: 

The difference:

std::cout << std::endl;

// is equivalent too

std::cout << "\n" << std::flush();

Use std::endl if you are in a command line app and want to guarantee that the user can see the output immediately.

Use "\n" if you are in a worried about performance (which is probably not the case if you are using the << operator).

I use std::endl mostly out of habit.

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

Martin York
+1  A: 

Not a big deal, but endl won't work in boost::lambda.

(cout<<_1<<endl)(3); //error

(cout<<_1<<"\n")(3); //OK , prints 3
Comptrol
A: 

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

Nathan
I don't want to seem rude, but that's pretty obvious. If you were to make them equivalent, you'd get two function calls each.
GMan
+1  A: 

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::clog << "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.

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

smerlin
Urgh! Who would ever want to be `using namespace std;` ?? :-)
Steve Folly
Nasty. Thanks for the comment, I'm sure others will run into that.
Head Geek