Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::endl — newline and stream-buffer flush

std::endl inserts a newline and calls flush() on an output stream. Unlike '\n', it explicitly requests buffer synchronization; use it when you need both operations.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

os << std::endl;
// Equivalent calls:
os.put(os.widen('\n'));
os.flush();

std::endl is an output-stream manipulator declared in <ostream>. Its usual form is os << std::endl, without parentheses after the manipulator name. It is a function template supporting different stream character types.

It calls os.put(os.widen('\n')) followed by os.flush(), then returns os. widen adapts the character to the stream's character type. The returned reference allows further << operations to be chained.

'\n' denotes a newline character; inserting it does not itself explicitly request a buffer flush. std::flush requests a flush without adding a newline. When you only need a line break, choose '\n' to avoid forcing additional synchronization for every line.

flush() synchronizes the stream buffer; it does not guarantee durable storage on disk. Output operations can fail, set an error state, and — depending on the exception mask — throw an exception. The example checks the state of std::cout, but the visible text alone does not establish when flushing occurred.

Examples

#include <iostream>

int main() {
    std::cout << "Ready" << std::endl;
    std::cout << "Done\n";
    return std::cout ? 0 : 1;
}

Result

Ready
Done

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources