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
- Problem with scanf() when there is fgets()/gets()/scanf() after it
- How to use getline() in C++ when there are blank lines in input?
- Basic Input / Output in C++
- Clearing The Input Buffer In C/C++
- C/C++ Preprocessors
- Common Memory/Pointer Related bug in C Programs
- Format specifiers in C
- Implicit initialization of variables with 0 or 1 in C
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Left Shift and Right Shift Operators in C/C++