What's the difference between cout << endl vs cout << “\n” in C++

Reference:

stackoverflow: C++: “std::endl” vs “\n”

stackoverflow: endl and flushing the buffer


The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for.

The only difference is that std::endl flushes the output buffer, and '\n' doesn't. If you don't want the buffer flushed frequently, use '\n'. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl.

What is meant by "flushing the buffer" here?

Output is generally buffered before it's written to the intended device. That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character. Flushing means emptying the buffer and actually writing it to the device.

results matching ""

    No results matching ""