C++ Academy · Lesson

Formatting Output with iomanip

Control width, precision, alignment, and number bases using the iomanip header.

Lesson 2 of 413 steps

Formatting Output with iomanip is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

The iomanip Header

The <iomanip> header provides manipulators — special objects you stream into cout to change its formatting state.

std::setw: Field Width

std::setw(n) sets the minimum width for the next field only. Shorter values are padded.

#include <iomanip>
std::cout << std::setw(10) << "Name" << std::setw(5) << "Age\n";
std::cout << std::setw(10) << "Ada"  << std::setw(5) << 36 << "\n";

std::setfill: Padding Character

By default setw pads with spaces. Change the fill character with setfill — the setting is sticky.

std::cout << std::setfill('0') << std::setw(4) << 42;
// 0042

std::left and std::right

Align text inside a setw field. Default is right-aligned.

std::cout << std::left << std::setw(10) << "Ada" << "|\n";
std::cout << std::right << std::setw(10) << "Ada" << "|\n";

std::setprecision

Control the number of significant digits or, with std::fixed, decimal places.

double pi = 3.14159265;
std::cout << std::setprecision(4) << pi << "\n";  // 3.142
std::cout << std::fixed << std::setprecision(2) << pi; // 3.14

Fixed Scientific Hexfloat

Switch between number formats with std::fixed, std::scientific, and std::hexfloat.

double x = 12345.6789;
std::cout << std::scientific << x << "\n"; // 1.234568e+04
std::cout << std::fixed << x << "\n";      // 12345.678900

Bases for Integers: hex oct dec

Print integers in different bases. std::showbase adds the prefix (0x, 0).

int n = 255;
std::cout << std::hex << n << "\n";  // ff
std::cout << std::oct << n << "\n";  // 377
std::cout << std::dec << n << "\n";  // 255 (default)

std::boolalpha

Print booleans as words instead of 1/0.

std::cout << std::boolalpha << true << "\n";  // true
std::cout << std::noboolalpha << true << "\n"; // 1

Manipulators Are Sticky

Most manipulators stay in effect until changed. setw is an exception — it applies only to the next field.

std::quoted for Safe Strings

std::quoted from <iomanip> wraps a string in double quotes and escapes embedded quotes. Perfect for log lines.

std::string name = "Ada \"Countess\"";
std::cout << std::quoted(name);
// "Ada \"Countess\""

C++20 std::format

Modern C++20 replaces most iomanip use with std::format — a printf-style API that is type safe.

#include <format>
std::cout << std::format("{:>10} | {:.2f}\n", "pi", 3.14159);

Quick Check

Which manipulator must be reapplied for each output field?

Recap

<iomanip> offers manipulators for width, precision, alignment, base, and quoting. setw is per-field; everything else is sticky. In C++20, prefer std::format for cleaner formatting.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Formatting Output with iomanip” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Formatting Output with iomanip”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Formatting Output with iomanip”?

Control width, precision, alignment, and number bases using the iomanip header. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Formatting Output with iomanip” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Using std::cin and std::cout Properly
  2. Formatting Output with iomanip
  3. Reading Input Safely with getline
  4. Working with std::cerr and std::clog
← Back to C++ Academy