I understand that the only difference between the two ways of making a new line is that the first one also empties the output buffer. Could you show me a practical example where you can see the difference in using one and the other?
I understand that the only difference between the two ways of making a new line is that the first one also empties the output buffer. Could you show me a practical example where you can see the difference in using one and the other?
Taken from here and here , explaining the following:
The
'\n'
is a character that, called a "line feed" or "lone feed", is represented by appearing null, but it just positions the cursor on a new line to continue typing.While it
std::endl
is the equivalent of:std::cout << "\n" << std::flush
.Their differences are strictly obvious, since calling character
\n
only involves concatenating a single string, but making use ofstd::endl
calls a buffer cleanup (which answers your question) .The cases where they can be used, let's try with the code of the first link:
With the following settings:
Returns the following results:
While the same code only with the escape:
And the same settings:
Produces these results:
Response times are much more efficient since the buffer is not "forced" to be cleared.
This, of course, in a hypothetical case where you do not need to clean the output buffer, but as I mentioned before, there is not much difference between the two, an escape character will always be printed
\n
for the new line. Which leaves one last detail: The only difference is performance.I hope it helps you.