endl vs \n in C++

Although they both seem to do the same thing, there is a subtle difference between them.

cout << endl  : Inserts a new line and flushes the stream

cout << "\n"  : Only inserts a new line.


cout << endl;

can be said equivalent to

cout << ‘\n’ << flush;

As a conclusion, cout << “\n” seems performance wise better than cout << endl; unless flushing of stream is required.


This article is contributed by Akshat Saxena. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up

Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:

1.8 Average Difficulty : 1.8/5.0
Based on 37 vote(s)

User Actions