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!
Print text, numbers and variables
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::endlinserts 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.
Print other types
- A
charprints as the character it holds.std::cout << 'A';printsA. 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
boolprints as1or0by default. Addstd::boolalphato printtrueorfalse. - A
std::stringprints 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::fixedandstd::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 thatcoutis not a member ofstd. - Missing
std::. Withoutusing namespace std;orusing std::cout;, a barecoutgives 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.coutis 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 addedstd::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 writestd::cout << value;to print. - Chain several values with
<<. - Use
'\n'for new lines, andstd::endlwhen you want to flush. - Use
std::cerrfor error messages. - Use
<iomanip>to control precision and width.