why do we use endl instead of \n more specifically in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why do we use endl instead of \n more specifically in c++

3rd Jun 2020, 5:31 PM
Lekhana Pinninti
Lekhana Pinninti - avatar
2 Answers
+ 5
endl appends '\n' to the stream and calls flush() on the stream. So cout << x << endl; is equivalent to cout << x << '\n'; cout.flush(); A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of cout you may not notice the difference since it's somehow synchronized (tied) with cin, but for an arbitrary stream, such as file stream, you'll notice a difference in a multithreaded program, for example. Here's an interesting discussion on why flushing may be necessary.
3rd Jun 2020, 5:34 PM
Abdulsalam Al-Ashwal
Abdulsalam Al-Ashwal - avatar
+ 3
Lekhana Pinninti https://www.sololearn.com/Discuss/1956685/?ref=app This post, answer by swim would be good for this question.
3rd Jun 2020, 5:33 PM
Preity
Preity - avatar