This question already has an answer here:
- C++: “std::endl” vs “\n” 12 answers
It was many years now since I stopped using std::endl
to end lines when writing to std::cout
, and started using "\n"
instead.
But now I start seeing more snippets of code using '\n'
instead, and I started wonder what might be best.
Besides the obvious that one is a string, and the other a character, is there any advantage to using this:
std::cout << variable << '\n';
Over this:
std::cout << variable << "\n";
Late addition:
When I asked this question I seemed to think that newline '\n'
flushed the buffer. Now I know that it depends.
By default std::cin
is tied to the old C stdin
FILE*
stream, and std::cout
is tied to stdout
. The flushing on newline comes from this tying. By default stdout
, if connected to a terminal, is line-buffered. That means a new line will flush its buffers. So when printing a newline using std::cout
, that will lead to stdout
being flushed.
If stdout
is not connected to a terminal (for example the output has been redirected or is piped), or if the tie between std::cout
and stdout
is broken, then newlines will not flush anything.
Actually,
'\n'
should be the default. Unless you want to also explicitly flush the stream (and when and why would you want to do that?), there is no need to usestd::endl
at all.1Of course, many books and tutorials use
std::endl
as the default. That is unfortunate and might lead to serious performance bugs.I suppose there's little difference between using
'\n'
or using"\n"
, but the latter is an array of (two) characters, which has to be printed character by character, for which a loop has to be set up, which is more complex than outputting a single character. Of course, when doing IO this rarely matters, but if in doubt, when you want to output one character literal, output a character literal, rather than a whole string literal.A nice side-effect of doing so is that you communicate in your code that you intended to output only a single character, and not just accidentally did this.
1 Note that
std::cout
is tied tostd::cin
by default, which leads tostd::cout
being flushed before any input operation, so that any prompt will be printed before the user has to input something.They do different things.
"\n"
Outputs a newline (in the appropriate platform-specific representation, so it generates a"\r\n"
on Windows), butstd::endl
does the same and flushes the stream. Usually, you don't need to flush the stream immediately and it'll just cost you performance, so for the most part there's no reason to usestd::endl
.std::cout << variable << std::endl;
std::endl
output a newline, but it also flushes the output stream. In other words, same effect asstd::cout << variable << '\n';
'\n'
output a newline for achar,
henceostream& operator<< (ostream& os, char c);
will be used.std::cout << variable << "\n";
"\n"
is aconst char[2]
, soostream& operator<< (ostream& os, const char* s);
will be used. We can imagine, this function will contain a loop, we might argue is overkill to just print out a newline.std::endl
flushes the stream. When this something you want to happen -- e.g. because you expect your output to be made visible to the user in a timely fashion -- you should usestd::endl
instead of writing'\n'
to the stream (whether as an isolated character or part of a string).Sometimes, you can get away without explicitly flushing the stream yourself; e.g. in a linux environment, if
cout
is synchronized withSTDOUT
(this is the default) and is writing to a terminal, then by default, the stream will be line buffered and will automatically flush every time you write a new line.However, it is risky to rely on this behavior. e.g. in the same linux environment, if you decide to run your program with
stdout
being redirected to a file or piped to another process, then by default, the stream will be block buffered instead.Similarly, if you later decide to turn off synchronization with stdio (e.g. for efficiency), then implementations will tend to use
iostream
's buffering mechanisms, which doesn't have a line buffering mode.I have seen much wasted productivity due to this mistake; if output should be visible when it is written, then you should either use
std::endl
explicitly (or usestd::flush
orstd::ostream::flush
, but I usually findstd::endl
more convenient), or do something else that ensures flushing happens often enough, such as configuringstdout
to be line buffered (assuming that's adequate).Edit: I worded my answer poorly, which may have lead people to believe that I thought "\n" actually printed a null character. This is of course wrong :)
Edit 2: Having looked at a C++ reference,
char
s are passed by reference anyway, so there's no difference there. The only difference is that the cstring will have to be searched for a delimiting character. The below isn't correct due to this fact.'\n'
would be ever so slightly more efficient than"\n"
, because the latter also contains a null character on the end, which means you're sending achar*
tooperator<<()
(usually 4 bytes on a 32-bit system) as opposed to a single byte for achar
.In practice, this is borderline irrelevant. Personally, I follow the convention that Vladimir outlined.*
There are no the best. You use what you need :
std::endl
- to end the line and flush the stream