C++ Output

Programs need a way to show results to the user. In C++ you print text and values to the screen using cout. This page shows you how to output text, numbers, and multiple values in a single line.

Printing with cout

The cout object (pronounced "see-out") is used to send output to the screen. You pair it with the insertion operator << , which you can think of as an arrow pointing the data toward the output.

Basic output

#include <iostream>
using namespace std;

int main() {
  cout << "C++ is powerful!";
  return 0;
}

Printing text and numbers

Text values must be wrapped in double quotes, but numbers are written without quotes. You can chain several values together by using the << operator more than once in the same statement.

Combining values

#include <iostream>
using namespace std;

int main() {
  cout << "I have " << 3 << " apples";
  return 0;
}

Text vs numbers

ValueHow to write itExample
TextInside double quotes"Hello"
Whole numberNo quotes42
Decimal numberNo quotes3.14
  • Use one << for each separate value you want to print.
  • Text inside quotes is printed exactly as written, including spaces.
  • Numbers are printed as their calculated value, so cout << 2 + 2 prints 4.
Note: By default, cout does not add a space or a new line between values. If you print two things in a row, they appear right next to each other unless you include spaces yourself.

Exercise: C++ Output

Which object is used to print text to the console in C++?