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

C++ Format Specifiers

C++ lets you control how numbers appear, like showing decimals, hexadecimal, or scientific notation, like choosing how to write down a measurement. Stream tools do the formatting.
Syntax
cpp
#include <iomanip>

std::cout << std::fixed << std::setprecision(digits) << decimal_value;

std::cout << std::hex << integer_value;  // or std::oct, std::dec

Floating-Point Formatting

The fixed stream manipulator forces floating-point numbers to print in standard decimal notation rather than switching to scientific notation for very large or small values.

Combine it with setprecision to control exactly how many digits appear after the decimal point, which is essential for anything like displaying money.

Example: Floating-Point Formatting

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;
}

Hexadecimal and Octal Formats

The hex and oct manipulators switch how subsequent integers print — hex shows them in base-16 and oct in base-8 — while dec switches back to the normal base-10 you'd expect.

These are especially useful when debugging low-level code, where seeing a value's hexadecimal form makes bit patterns and memory addresses much easier to read.

Example: Hexadecimal and Octal Formats

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

// Program execution starts in main()
int main() {
	// Declare value and set it to 255
	int value = 255;
	// Print to the console with cout
	std::cout << std::hex << value << std::endl;
	// Print to the console with cout
	std::cout << std::oct << value << std::endl;
	std::cout << std::dec << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Boolean Output Format

By default C++ prints boolean values as the raw integers 1 and 0, which can be confusing in output meant for a human reader.

Applying boolalpha makes cout print the actual words true and false instead, and noboolalpha switches back to the numeric form if you need it again later.

Example: Boolean Output Format

cpp
#include <iostream>

int main() {
	bool active = true;
	std::cout << active << std::endl; // 1
	std::cout << std::boolalpha << active << std::endl; // true
	return 0;
}

Scientific Notation

scientific formats floating-point numbers using exponential notation, like 1.5e+08 instead of 150000000, which keeps very large or very small numbers compact and easy to compare at a glance.

This matters in scientific or engineering code where values can span many orders of magnitude in the same output.

Example: Scientific Notation

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

// Program execution starts in main()
int main() {
	// Declare bigNumber and set it to 150000000.0
	double bigNumber = 150000000.0;
	// Print to the console with cout
	std::cout << std::scientific << bigNumber << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base Prefix Formatting

showbase adds a visible prefix that identifies a number's base — 0x for hexadecimal and a leading 0 for octal — so a reader doesn't have to guess how to interpret the digits that follow.

It's most useful alongside hex or oct, since without it a hex value like ff could easily be mistaken for decimal.

Example: Base Prefix Formatting

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

// Program execution starts in main()
int main() {
	// Declare value and set it to 255
	int value = 255;
	// Print to the console with cout
	std::cout << std::showbase << std::hex << value << 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. Looking for printf style specifiers like %d in cout, when C++ streams use manipulators such as hex and fixed instead.
  2. Setting fixed and setprecision and expecting them to reset, when they stay in effect for later output.
  3. Printing a bool and expecting true, when you need boolalpha to show text instead of 1 or 0.
🔒

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.