I've read that '\n' is preferred over 'endl' for new line in c++ because 'endl' inserts new line and flushes the stream. Please tell me what is flushing the stream ?
3 Answers
Flushing a stream ensures that all data that has been written to that stream is output, including clearing any that may have been buffered.
Some streams are buffered to aid performance, e.g. a stream writing to disk may buffer until the content reaches a block size.

- 54,545
- 11
- 67
- 105
When you flush the stream you force contents of the output stream to the default output medium the OS uses. The term stream is an abstraction of a construct that allows you to send or receive an unknown number of bytes. In certain points in a program, the output stream is automatically flushed, flushing is not always necessary. To reduce overhead and improve performance, a stream buffers its contents and only periodically "flushes" it. Examples of streams are cin (std::cin) and cout (std::cout) which are the input and output streams. "std::cin" has a buffer to store input data whereas "std::cout's" buffer is used to store data that's going to be sent to screen.

- 1,779
- 19
- 25
Flushing a stream is more likely as Flushing the toilet, where in toilet Flushing the toilet is cleared and in Stream(I/O Stream) Flushing the "Buffer-memory" is cleared.(where here Buffer-memory is a memory which is used to store the data temporarily from/for the I/O streams).
So, everytime "Flushing the Stream(means using of "endl" )" might become the bottleneck of the program, means your program always slows down at this point.
for more on buffer visit : this

- 711
- 9
- 9
-
2An interesting metaphor makes it so clear and easy to understand. – Roy Ling May 06 '22 at 06:21