← Back to C++ Course | Chapter 2: Input & Output | Lesson 2 of 8

C++ Print Numbers

cout << can print numeric literals and the results of expressions directly, and setprecision/setw from iomanip offer extra control over decimal places and field width.
Syntax
cpp
std::cout << number << std::endl;

std::cout << number1 operator number2 << std::endl;

Printing Numeric Literals

cout << can print numeric literals directly, without any conversion, and will display the number exactly as written, whether it's a whole integer or a decimal value.

Example: Printing Numeric Literals

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << 42 << std::endl;
	// Print to the console with cout
	std::cout << 3.14 << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing Arithmetic Results

When cout is given a mathematical expression, C++ evaluates that expression completely first and only then prints the single resulting number, not the original expression text.

Example: Printing Arithmetic Results

cpp
#include <iostream>

int main() {
	std::cout << 5 + 3 << std::endl; // prints 8, not "5 + 3"
	return 0;
}

Printing Different Number Types

cout << works with every numeric type in C++ -- short, int, long, float, and double -- and prints each one formatted according to that type's own conventions.

Example: Printing Different Number Types

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare s and set it to 10
	short s = 10;
	// Declare i and set it to 1000
	int i = 1000;
	long l = 100000L;
	float f = 1.5f;
	double d = 3.14159;
	// Print to the console with cout
	std::cout << s << " " << i << " " << l << " " << f << " " << d << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Concatenating Numbers with Text

The << operator can be chained multiple times in a single cout statement, mixing string literals and numeric values, and any number combined this way is automatically converted to its printable text form.

Example: Concatenating Numbers with Text

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 95
	int score = 95;
	// Print to the console with cout
	std::cout << "Score: " << score << " out of 100" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Formatting Numbers in Output

For more control over how a number appears, the iomanip library provides manipulators like setprecision and fixed for a fixed number of decimal places, and setw for a minimum field width.

Example: Formatting Numbers in Output

cpp
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>

// Program execution starts in main()
int main() {
	// Declare price and set it to 19.5
	double price = 19.5;
	// Print to the console with cout
	std::cout << std::fixed << std::setprecision(2) << price << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting cout << 5 / 2 to print 2.5, when both are ints so it prints 2.
  2. Writing cout << "Total: " + 5;, which does pointer arithmetic on the string instead of joining text and a number.
  3. Forgetting that cout << 10 + 5 << 3 prints 153 as separate values, rather than adding everything together.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.