When printing output to a terminal in C++, should i use "\n", or "endl"??? Is one lighter, faster, or industry standard?

I use a mixture of both, if you stick a "\n" in a file it will put the next bit of data on a newline. I think endl is just for console output. its probably good to get used to using "\n" as there are other escape sequences too, like \t (tab) \b (backspace) \" (double quotes) ect. :)

>When printing output to a terminal in C++, should i use "\n", or "endl"???
I recommend using '\n'. For the most part, the stream is flushed in the right places by default, so explicitly flushing it yourself is just an extra unnecessary step.

>Is one lighter, faster, or industry standard?
These two statements are directly equivalent:

cout<<"Hello, world!"<<endl;
cout<<"Hello, world!\n"<<flush;

endl prints a newline character and then forces a flush of the stream. So just printing '\n' is lighter and faster because the flush isn't performed.

Also depends on how much you are printing. Using endl can significantly slow your code if you are printing alot.

As an example when I started out with c++ I did some exercises on projecteuler.net, constantly printing in a while loop, I was blown away when at some point I decided to use '\n', and the algorithm finished in a mere seconds as opposed to minutes.

Comments
It was a great post...a lot of help :D

Also depends on how much you are printing. Using endl can significantly slow your code if you are printing alot.

As an example when I started out with c++ I did some exercises on projecteuler.net, constantly printing in a while loop, I was blown away when at some point I decided to use '\n', and the algorithm finished in a mere seconds as opposed to minutes.

Now that's the answer i was looking for :icon_mrgreen:

*adds one to his rep* :)

This question has already been answered. Start a new discussion instead.