0Pricing
C++ Academy · Lesson

Formatting Output with iomanip

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

Formatting Output with iomanip is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Formatting Output with iomanip” lesson free?

Yes — the full text of “Formatting Output with iomanip” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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