0

\n and endl

Why cant we use \n instead of endl? I still do not get the difference.

17th Feb 2019, 7:37 AM
Rishita
Rishita - avatar
5 Réponses
+ 7
You can use \n, but it has to be part of the string: cout << "This is a string\n", not cout << "This is a string" << \n. For the compiler, "\n" is just a regular ASCII character like "A", "J" or "
quot;, so it has to be in quotation marks.
17th Feb 2019, 7:51 AM
Anna
Anna - avatar
+ 5
endl prints new line and flushes the stream but \n only prints new line cout << endl is equivalent to cout << "\n" << flush
17th Feb 2019, 7:53 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
I think you mean "\n" and they are used to do the same thing, it just depends on what one you prefer to use
17th Feb 2019, 7:41 AM
LynTon
LynTon - avatar
+ 4
Their are not really same.... endl is a representation of newline on your platform than can be \n (Windows) or \r\n (Unix) for text files while using \n you will put into stream \n without taking cure of platform. Beside this, endl flush the stream opposite to \n
17th Feb 2019, 7:59 AM
KrOW
KrOW - avatar
0
Yes! Sorry right \n. But when I replace endl with \n it shows error, why?
17th Feb 2019, 7:44 AM
Rishita
Rishita - avatar