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

C++ iomanip Library

The iomanip library gives tools to make output line up neatly, like setting column widths in a table. You can pad, align, and choose decimal places.
Syntax
cpp
#include <iomanip>

std::cout << std::setw(width) << value;

std::cout << std::setfill('fill_char') << std::setw(width) << value;

Setting Width with setw

setw(n) reserves a fixed-width field of n characters for the very next value printed, padding it with spaces so columns of output line up neatly.

It only affects the single next output operation, so you need to call it again before each value if you want a whole table of aligned columns.

Example: Setting Width with setw

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

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

Setting Fill Characters with setfill

setfill(c) changes what character fills the empty space setw leaves behind — spaces by default, but you might use 0 to zero-pad numbers or '*' for a visual separator.

It stays in effect for all subsequent setw calls until you change it again, unlike setw which only applies once.

Example: Setting Fill Characters with setfill

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

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

Floating Point Precision

setprecision(n) caps how many significant digits (or decimal places, when combined with fixed) are shown for floating-point output, which prevents long, messy strings of digits like 3.14159265358979.

This is essential any time you're displaying computed values like averages or percentages to a user.

Example: Floating Point Precision

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

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

Left and Right Alignment

left and right control which side of a setw-defined field the text sticks to — left pushes text to the start with padding trailing after it, while right (the default) pads before the text so it aligns against the field's right edge.

Switching between them is what lets you build both left-aligned labels and right-aligned numbers in the same table.

Example: Left and Right Alignment

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

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << std::left << std::setw(10) << "Name" << std::endl;
	// Print to the console with cout
	std::cout << std::right << std::setw(10) << "Score" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Fixed Point Notation

Combining fixed with setprecision(2) locks floating-point output to exactly two decimal places regardless of the actual value's precision, which is exactly the format expected for displaying currency like $19.99.

Without fixed, setprecision alone controls total significant digits rather than decimal places specifically, which can produce unexpected results for values like 100.5.

Example: Fixed Point Notation

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.99
	double price = 19.99;
	// 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. Forgetting #include <iomanip>, so setw and setprecision are undefined.
  2. Expecting setw to persist, when it only applies to the next output item.
  3. Using setprecision(2) without fixed and getting significant digits instead of two decimal places.
🔒

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.