Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::cout: writing standard output

std::cout is an output-stream object declared in iostream. The << operator sends it values, using formatting appropriate to their types.

Version: C++23

Syntax

std::cout << value;

Include <iostream> and use std::cout << value. std::cout is an object, not a function. Operations can be chained because each returns the same stream. Here << means stream insertion; in a different expression with integers it may mean a bit shift.

'\n' writes a newline, while std::endl additionally requests a buffer flush. Do not force a flush after every line without a reason. Output can be redirected to a file or another program, so writing does not guarantee immediate display on a screen.

Manipulators change formatting: std::boolalpha displays booleans as words, and std::fixed with std::setprecision(2) writes two digits after the decimal separator. This stream setting does not round the stored variable; some settings persist across later operations. Custom types do not automatically support output. Detect a failed write with if (!std::cout); do not assume that every output operation succeeds.

Examples

#include <iostream>
#include <iomanip>

int main() {
    std::cout << "sum=" << (2 + 3) << '\n';
    std::cout << std::boolalpha << true << ' ' << false << '\n';
    std::cout << std::fixed << std::setprecision(2) << 1.5 << '\n';
}

Result

sum=5
true false
1.50

Sources