+ 5

Endl vs \n

is there a benefit for using one over the other besides making code cleaner?

13th Dec 2016, 5:48 AM
Corey
5 Réponses
+ 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
9th Oct 2017, 11:35 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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";
13th Dec 2016, 6:10 AM
Franky BrainBox
Franky BrainBox - avatar
+ 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
13th Dec 2016, 6:33 AM
manish rawat
manish rawat - avatar
+ 1
"\n" is an escape key. A shortcut for "endl";
24th Feb 2017, 8:25 AM
Jerome Dass
Jerome Dass - avatar
+ 1
\n is NOT the same as endl, endl calls flush() look it up!
30th Dec 2017, 10:17 PM
jonas