tags:

views:

130

answers:

3
+11  A: 

endl flushes the buffer, where \n does not. Otherwise, they are the same.

endl is good, because if the program crashes, your stuff gets printed. BUT, there is a performance hit with endl.

Starkey
Will `endl` translate to `\n` or `\n\r` depending on the platform the program is compiled?
Pablo Santa Cruz
@Pablo Santa Cruz - Yes, `endl` translates correctly.
Starkey
Yes, but so will plain '\n', even if it's embedded in the middle of a string. That's handled 'way down in the guts of streambufs, IIRC.
Zack
So where are the best places to use endl to not kill performance yet let it do what its suppose to do?
Alec
@Alec - In general, writing output kills performance (regardless of `endl` or `\n`). I personally try to use `endl` just so the stuff gets flushed and not lost during a crash. You'd have to do your own tests to see if the performance hit is critical to your application. If you were printing several lines, you could limit the `endl` to the last line to help with performance.
Starkey
+2  A: 

dupe http://stackoverflow.com/questions/213907/c-stdendl-vs-n

brumScouse
worth pointing out the dupe, but not worth points, can I be downvoted please.
brumScouse
A: 

endl is a manipulator that inserts a new-line character. Additionally, for buffered streams, endl flushes the buffer (i.e. writes all unwritten characters in the buffer to the output sequence, see ostream::flush).

And '\n' is actually a new-line character.

kilotaras