cout

How to Use cout in C++

Learn how to use cout in C++ to print text, numbers and variables, add new lines, and format output, with short complete examples you can compile and run.

On this page
  1. What cout does
  2. Include the header
  3. Print text, numbers and variables
  4. Start a new line
  5. Print other types
  6. Format numbers and text
  7. Send error messages to cerr
  8. Common mistakes
  9. Other ways to print
  10. Quick recap

What cout does

std::cout is the output stream that C++ programs use to print to standard output, which is usually the terminal. You send values to it with the << operator, and it writes them in the order you give them.

The short answer: std::cout << "Hello"; prints the text Hello.

Include the header

To use cout, include the <iostream> header at the top of your file. Here is a complete program:

#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

The name cout belongs to the std namespace, so the full name is std::cout. Many tutorials add using namespace std; so they can write cout alone. Whether to use that shortcut is a separate decision. For the trade-offs, see c using namespace std.

Compile and run the program with a C++ compiler. With GCC, for example:

g++ -std=c++17 -Wall hello.cpp -o hello
./hello

The output is:

Hello, world!

Use one << for each piece you want to print. This program prints a variable and a double:

#include <iostream>

int main() {
    int apples = 3;
    double price = 1.25;

    std::cout << "You have " << apples << " apples." << '\n';
    std::cout << "Each costs $" << price << '\n';
    return 0;
}

The output is:

You have 3 apples.
Each costs $1.25

Numbers print in their normal form. By default, a double shows up to six significant digits, so 3.14159265 prints as 3.14159. The formatting section below shows how to change that.

Start a new line

There are two common ways:

  • '\n' inserts a newline character.
  • std::endl inserts a newline and flushes the output.
std::cout << "First line" << '\n';
std::cout << "Second line" << std::endl;

Output sent to cout is often held in a buffer and written in chunks. Flushing with std::endl pushes that text out right away, which can help when you want a message to appear before the program waits for input. For ordinary output, especially inside loops, '\n' is the usual choice, because flushing on every line adds some overhead.

  • A char prints as the character it holds. std::cout << 'A'; prints A. Single quotes are for one character, and double quotes are for text. If you need the number stored in a char, see c char to int.
  • A bool prints as 1 or 0 by default. Add std::boolalpha to print true or false.
  • A std::string prints its text. Include <string> to use it. For ways to work with the text itself, see c string methods.
#include <iostream>
#include <string>

int main() {
    std::string name = "Ada";
    bool is_ready = true;

    std::cout << name << '\n';
    std::cout << is_ready << '\n';
    std::cout << std::boolalpha << is_ready << '\n';
    return 0;
}

The output is:

Ada
1
true

For the full list of built-in types, see primitive data types in c.

Format numbers and text

The <iomanip> header provides helpers that change how values print:

#include <iomanip>
#include <iostream>

int main() {
    double pi = 3.14159265;

    std::cout << std::fixed << std::setprecision(2) << pi << '\n';
    std::cout << std::setw(8) << "right" << '\n';
    return 0;
}

The output is:

3.14
   right
  • std::fixed and std::setprecision(2) control how doubles print. They stay in effect for later output.
  • std::setw(8) sets the width of the next item only. Text shorter than the width is padded with spaces on the left, so it lines up on the right.

Send error messages to cerr

std::cerr is a separate stream for error messages. It writes to standard error, and many shells still show that text on the screen when standard output goes to a file.

std::cerr << "Could not open the file" << '\n';

Common mistakes

  • Missing #include <iostream>. The compiler usually reports that cout is not a member of std.
  • Missing std::. Without using namespace std; or using std::cout;, a bare cout gives the same kind of error.
  • Using a comma instead of <<. std::cout << "Total: ", total; does not print the second value.
  • Forgetting the semicolon at the end of the statement.
  • Mixing up quotes. Double quotes are for text, and single quotes are for one character.

Other ways to print

  • printf, from <cstdio> in C++ (or <stdio.h> in C), uses format codes such as %d. cout is the more common choice in C++ because it works from the type of each value you pass to <<.
  • C++20 added std::format, and C++23 added std::print. Support depends on your compiler and standard library, so check before you rely on them.
  • For reading input from the user with std::cin, see c input string.

Quick recap

  • Include <iostream> and write std::cout << value; to print.
  • Chain several values with <<.
  • Use '\n' for new lines, and std::endl when you want to flush.
  • Use std::cerr for error messages.
  • Use <iomanip> to control precision and width.

Keep exploring

Guides from other topics

All topics →

Enums in C++ With Examples

Learn enums in C++ with clear examples: syntax, automatic values, enum class, switch statements, underlying types, and common mistakes to avoid.

Enums 5 min read