+ 3

What is the difference between 'endl' and '\n' tag??

12th Oct 2017, 7:26 AM
Sagar Paul
Sagar Paul - avatar
4 Answers
+ 17
"\n" is a scape sequence (a string of length 1) which inherited from C and cause a new line operation. "endl" is an object which does the same thing as "\n" and also flushes standard output buffer. You probably ask what do you mean by flushing the standard output. The following link leads you through some examples. [https://www.quora.com/What-does-it-mean-to-flush-the-output-stream-in-C++]
12th Oct 2017, 8:27 AM
Babak
Babak - avatar
+ 4
Not the same thing!!! endl forces a flush on the stream on top of adding the newline character, while just '\n' by itself does not force any flushes. You should know when to flush a stream and when not to, because it can effect performance drastically
12th Oct 2017, 8:08 AM
aklex
aklex - avatar
+ 1
Yes, they're different. "\n"Ā ----is just a string of length 1 that gets appended to stdout. std::endl, ----instead, is an object that will cause to append the newline character ("\n") AND to flush stdout buffer. For this reason it will take more processing.
13th Oct 2017, 3:34 PM
Ravish kumar singh
Ravish kumar singh - avatar