-2

So, I just got into 'C++' dev, and I am wondering how, exactly std::endl and \n differ. I looked it up, and I think endl flushes the buffer, which, I believe, means its no longer being temporarily stored in memory, and is now being printed out into a text file or a terminal. My question, though, is whether you can use \n for console applications too, which is printing onto a terminal, and it outputs the same. Is my interpretation of the buffer and flushing wrong? If so, what exactly does these mean?

EDIT: The question is mainly about how \n can behave the same way in the terminal if things are only outputted onto the terminal if they've been flushed.

skyarex
  • 19
  • 1
  • 4

1 Answers1

2

Using std::endl is not the only thing that flushes cout.

Reading from cin will also cause the cout buffer to be flushed, so that prompts can be visible on the console. By default the streams are "tied" to each other.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • You may also want to note that `std::flush` also flushes the buffer (which is what `std::endl` does internally after outputting the `\n`. – Jesper Juhl Dec 31 '17 at 13:04