Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ std::cout object

Reference for std::cout: the << operator, chaining, put, write, flush, endl versus newline, formatting flags like boolalpha, hex and fixed, and printing chars.

std::cout (header <iostream>) is the global std::ostream connected to the program's standard output. You send values to it with the insertion operator <<. There is an overload for every built-in type, for std::string, std::string_view, C strings and many library types, and you can add overloads for your own.

Each << returns the stream, so several outputs chain in one statement. Output is buffered: characters collect in memory and reach the terminal when the buffer fills, when the stream is flushed or when the program ends normally.

C++
#include <iostream>

int main() {
    std::cout << "Total: " << 3 * 4 << '\n';
}

Try it

C++ Printing different types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Flags and manipulators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
extern std::ostream cout;

std::ostream& operator<<(std::ostream& os, const T& value);   // many overloads

std::ostream& put(char c);
std::ostream& write(const char* s, std::streamsize count);
std::ostream& flush();
Name Description
value The thing to print. Integers, floating point, bool, char, C strings, pointers, std::string, std::string_view, std::error_code, std::bitset, std::complex and more.
c A single character for put.
s, count A character array and the number of characters to write with write.

Returns: all of them return a reference to std::cout, which enables chaining.

Errors / undefined behavior: writing a null const char* is undefined behavior. A failed write sets badbit (check with cout.fail()); exceptions are not thrown unless you enable them with cout.exceptions(...). Operator precedence can surprise: cout << a & b does not do what it looks like, so parenthesize cout << (a & b).

Usage notes

  • Flush rules. cout is tied to cin (it flushes before a read) and cerr flushes it before writing. std::endl is '\n' plus a flush; std::flush flushes alone. Prefer '\n' in loops.
  • Sticky versus one-shot. std::hex, std::fixed, std::setprecision, std::boolalpha and std::left stay in effect until changed. std::setw applies to the next output only.
  • Default float format prints up to 6 significant digits, so 3.14159265 shows as 3.14159. Use setprecision or std::format when you need control.
  • Pointers. A const char* prints as text; other pointers print as an address. Cast to const void* to see the address of a char array.
  • char quirks. signed char and unsigned char (including uint8_t) print as characters; add unary + or cast to int for the number.
  • Thread safety. Concurrent << calls on cout do not cause data races (with default synchronization) but their output may interleave. Build the whole line first, or use std::println, or std::osyncstream.
  • Redirection. Standard output may be a file or pipe, where buffering is bigger and output order relative to cerr is no longer guaranteed.
  • Newer alternative. std::print / println (C++23) are faster in many cases and need no manipulators.