Does new line character also flush the buffer?

Converting comments into an answer.

It depends on where cout is going. If it goes to a terminal (‘interactive device’), then it can’t be fully buffered — it is usually line buffered, meaning that characters appear after a newline is printed, or could in theory be unbuffered. If it is going to a pipe or file or other non-interactive destination, the endl forces the data out even if the stream is fully buffered, as it usually will be.

I also wanted to know if I provided neither new line character nor endl, will the output be displayed on the stdout once it reaches the end of the program, I know it does for terminal, but is it applicable to all types of stdout?

Yes, when the file stream is closed at the (normal) end of the program, pending output will be flushed. It’ll also be flushed when the buffer is full. If the program aborts, pending output usually won’t be flushed.

Leave a Comment