-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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You may also want to note that std::flush also flushes the buffer (which is what std::endl does internally after outputting the \n.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.