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;
So cout << “\n” seems performance wise better than cout << endl; unless flushing of stream is required.
Some other differences between endl and \n are:
- endl is manipulator while \n is character.
- endl doesn’t occupy any memory whereas \n is character so It occupy 1 byte memory.
- 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.
- We can use \n both in C and C++ but, endl is only supported by C++ and not the C language.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Why we should avoid using std::endl
- Iterate over characters of a string in C++
- Python program to add two binary numbers
- Difference between int (*p) and int* p?
- 3-way comparison operator (Space Ship Operator) in C++ 20
- How to generate a vector with random values in C++?
- Brief Overview & Comparison of Object-Oriented Programming from C to Java
- Relational Database from CSV Files in C
- Difference between std::set vs std::vector in C++ STL
- Vector of Maps in C++ with Examples
- Data Conversion in C++
- Movie tickets Booking management system in Python
- Bug Tracking System
- C Program to sort rows of the Matrix