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 also in C++ but we can't use endl in 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
- Why we should avoid using std::endl
- Program to find the Nth Prime Number
- time.h header file in C with Examples
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Check whether Quadrilateral is valid or not if angles are given
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- std::hash class in C++ STL
- Find the sum of the first half and second half elements of an array
- Time taken per hour for stoppage of Car
- History of C++
- Check whether the triangle is valid or not if angles are given