cout

What Is std::cout << in C++?

Break down std::cout << in C++ piece by piece: the std namespace, the cout object, the insertion operator, and why several values can be chained in one line.

On this page
  1. The short answer
  2. The parts of the statement
  3. Why the std:: prefix is there
  4. Why << works here
  5. Chaining values
  6. Input uses the other direction
  7. Recap

The short answer

std::cout << value; sends value to standard output. Read it as “send this value to standard output.”

The parts of the statement

Part What it is What it does
std:: A namespace prefix Says that cout comes from the standard library
cout An object of type std::ostream The output stream that receives the data
<< The insertion operator Passes the value on its right to the stream on its left
value Any value you want to print A string literal, a variable or an expression

Here is a complete program that uses the statement:

#include <iostream>

int main() {
    int count = 7;
    std::cout << "Count: " << count << '\n';
    return 0;
}

The output is Count: 7.

Why the std:: prefix is there

The standard library places its names in a namespace called std, which keeps them apart from names in your own code. The std:: prefix tells the compiler where to find cout.

You can drop the prefix in two ways:

#include <iostream>

using std::cout;

int main() {
    cout << "Hello\n";
    return 0;
}

using std::cout; brings only that one name into scope. using namespace std; brings in the whole namespace. It is common in small examples, but many developers avoid it in larger code. For the trade-offs, see c using namespace std.

Why << works here

<< is the insertion operator. For integers, << is also the left-shift operator, so 1 << 3 equals 8. The standard library overloads << for std::ostream, so the same symbol works for text, integers, doubles and other types. Operator overloading lets a type define its own meaning for an operator, and the standard library uses it for output.

The same pattern works with string streams, such as std::ostringstream. See c string stream for that.

Chaining values

<< returns the stream it was given, so you can keep adding values to the same statement:

std::cout << "a" << 1 << '\n';

Each << works on the result of the one before it, from left to right. Here std::cout << "a" runs first, then << 1, then << '\n'.

Input uses the other direction

std::cin uses >> to read values in, for example std::cin >> age;. The idea is the same, but the arrows point the other way. For reading text, see c input string.

Recap

  • std::cout << value; sends a value to standard output.
  • std:: tells the compiler that cout comes from the standard library.
  • << is the insertion operator, and the standard library overloads it for many types.
  • Several values can be chained because each << returns the stream.
  • For more printing examples, see c cout.

The complete guide to cout

How to Use cout in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The Latest C++ Standard

C++23 is the most recent C++ standard at the time of writing, with C++26 next. Here is what changed, how versions are named, and how to compile for each.

C++ Standards 3 min read