Advantage of using endl over \n ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Advantage of using endl over \n ?

10th Aug 2016, 8:27 AM
Hitesh Datt
Hitesh Datt - avatar
2 Answers
+ 7
Both perform the same function of moving to the next line. \n of course has the added benefit that it's quicker to write, and can be used inside quotations, which can help reduce line sizes. The main difference between the two though, is that endl will clear the input stream (or the buffer), while \n will not. The input stream is basically a "queue" which will send anything that's at the front of the queue to things such as cin. cin takes the first thing inside the input stream, if the stream is empty, it waits for something to be put in there via user input. If you ever find inputs being skipped in a program, or you're getting unusual results, it's likely because something is inside the input stream, and is automatically being used once it comes across an input such as cin.
10th Aug 2016, 10:48 AM
Cohen Creber
Cohen Creber - avatar
- 5
the advantage of endl over \n is endl doesn't have to be inside the quotation
11th Aug 2016, 2:04 PM
jxy