LOGBOOK

HELP

Quiz Entry - updated: 2026.07.06

How do you output a newline in C++?

Both endl and "\n" end the line, but endl also flushes the output buffer while "\n" does not.

cout << "Hello" << endl;    // newline + flush
cout << "Hello" << "\n";    // just a newline (buffered)
cout << "Hello\n";          // same as above

The difference:

  • endl writes a newline and flushes the buffer (forces output to appear immediately)
  • "\n" only writes the newline character; the text may sit in the buffer a bit longer

When to use which:

  • endl — when you need the output to show up right away (interactive prompts, debugging before a crash)
  • "\n" — for performance, when writing a lot (logging, bulk output), since unnecessary flushing is slow

Tip: In most code "\n" is the better default — it's faster. Reach for endl only when an immediate flush actually matters.

Go deeper:

From Quiz: REVE1 / C++ Programming | Updated: Jul 06, 2026