+ 5
Endl vs \n
is there a benefit for using one over the other besides making code cleaner?
5 Answers
+ 14
Necro-ing. Because nobody here mentioned about flushing the buffer, and no, endl is not a macro. (wtf)
https://www.sololearn.com/discuss/650395/?ref=app
https://www.sololearn.com/discuss/624338/?ref=app
+ 4
There is no much difference but here goes this logic.
I use "endl" to create a new line when my cout prints an initialized or assigned variable at the end of cout statement..
example:-
int x = 6;
cout << x << endl;
I use "\n" to create a new line when i know my cout prints a string that is not initialized at then end of cout statement.
example:-
int age = 12;
cout << "I am " << age << " years old.\n";
+ 4
yes there is benefit.
use \n for faster.results.
what is endl, its just a macro defined as
endl "\n".
so when we use endl compiler have to use time to repalce endl with \n.
therefore for faster code \n is recommended
+ 1
"\n" is an escape key. A shortcut for "endl";
+ 1
\n is NOT the same as endl, endl calls flush() look it up!