Skip to content
Related Articles

Related Articles

Improve Article
endl vs \n in C++
  • Difficulty Level : Easy
  • Last Updated : 08 Feb, 2021

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.

Therefore, 
cout << endl; 
can be said equivalent to 
cout << ‘\n’ << flush; 
So cout << “\n” seems performance wise better than cout << endl; unless flushing of stream is required. 
Some other differences between endl and \n are:  

  1. endl is manipulator while \n is character.
  2. endl doesn’t occupy any memory whereas \n is character so It occupy 1 byte memory.
  3. \n being a character can be stored in a string(will still convey its specific meaning of line break) while endl is a keyword and would not specify any meaning when stored in a string.
  4. We cannot write endl in between double quotation while we can write \n in between double quotation like 
    cout<<“\n”; it is right but cout<<“endl”; is wrong.
  5. We can use \n both in C and C++ but, endl is only supported by C++ and not the C language.

Reference: 
http://stackoverflow.com/questions/213907/c-stdendl-vs-n 

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 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :