<<endl;

when is it necessary to use the <<endl;
Last edited on
When you want to output a newline and flush the stream
http://www.cplusplus.com/reference/iostream/manipulators/endl/
Is there a (big) difference between << endl; and <<"\n"; ?
Well, I asked this question because Ive seen alot of people do multiple cout<< calls without endl;

Nathan10 thats a good question, I was going to ask it myself.
closed account (3TkL1hU5)
@nathan10 As Bazzy said endl flushes the stream.

Other than that no, they do the same thing.
At risk of repeating everyone else...

std::cout<<std::endl; is equal to std::cout<<"\n"<<std::flush;
When you want to output a newline and flush the stream


I believe performance can be different. Since endl output a newline and flush the stream it is doing a lot of work. Flush to stream is not a cheap operation. Imagine my code has a lot of endl statements then for each line I will be flushing the stream.

In comparison, if I put a "\n", then it may or may not get flushed to stream. Most likely when the buffer is full would the OS flush them to stream. This mean a flush is executed not as frequent as endl.

I understand with modern computers, flushing a stream is a trivial and cheap operation but I still prefer my "\n" convention instead.
Topic archived. No new replies allowed.