Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

C++ stream insertion and extraction operators

Reference for the C++ stream operators << and >>: chaining, formatting with manipulators, reading with whitespace rules, and overloading them for your own types.

The same two symbols that shift bits also move data through streams. stream << value inserts (writes) a value into an output stream such as std::cout; stream >> variable extracts (reads) a value from an input stream such as std::cin. Both return the stream itself, so you can chain them: std::cout << a << b << '\n'.

The standard library overloads these operators for the built-in types, std::string, and many library classes. You teach them about your own types by writing a free operator<< or operator>>.

C++
std::cout << "x = " << 42 << '\n';
int age;
std::cin >> age;

Try it

C++ Writing and reading

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++ Stream operators for your own type

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++
std::ostream& operator<<(std::ostream& os, const T& value);
std::istream& operator>>(std::istream& is, T& value);

os << a << b;        // chained: (os << a) << b
is >> a >> b;
Name Description
os An output stream (std::cout, std::cerr, std::ofstream, std::ostringstream). Taken and returned by non-const reference.
is An input stream (std::cin, std::ifstream, std::istringstream).
value The data. For >> it must be a non-const reference so it can be filled.

Returns: a reference to the same stream, which allows chaining and testing (while (in >> x)).

Errors / undefined behavior: a failed extraction sets failbit (bad format) or eofbit (end of data) and, since C++11, writes 0 to a failed numeric target (or the maximum/minimum on overflow). Once a stream has failed, later operations do nothing until you call clear(). Extracting into a char[N] with no width (std::cin >> buf) was a buffer overflow risk; since C++20 the array overload limits itself to N - 1 characters, but std::string is still the right choice.

Usage notes

  • Free functions: because the stream is on the left, the operators are normally non-member functions. Make them friend if they need private data.
  • Return the stream: returning os or is is what allows chaining. Report input errors with setstate(failbit) instead of throwing.
  • Print only the object: no trailing newline or label; callers add their own separators.
  • Stream state: a loop while (in >> x) ends on failure or end of data. Use in.eof() after the loop to tell the cases apart if you need to. while (!in.eof()) is a classic bug that processes the last value twice.
  • Mixing >> and getline: >> leaves the newline in the buffer, so the next std::getline returns an empty line. Call std::cin.ignore(...) or read lines with getline and parse them with std::istringstream.
  • Formatting is sticky: std::hex, std::fixed and std::setprecision stay in effect until changed; std::setw applies only to the next value.
  • Precedence: << has the precedence of a shift: std::cout << a & b parses as (std::cout << a) & b; parenthesize bitwise expressions and conditional expressions.
  • Modern alternatives: std::print and std::println (C++23) and std::format (C++20) are shorter and faster for formatted output.
  • Shifts: for numbers, << and >> still mean bit shifts (Bitwise operators); the stream meaning applies only when the left operand is a stream.