Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ <iomanip> header

Reference for the C++ <iomanip> manipulators: setw, setfill, setprecision, setbase, setiosflags, quoted and get_money, with sticky versus one-shot rules.

<iomanip> declares the manipulators that take an argument: std::setw, std::setfill, std::setprecision, std::setbase, std::setiosflags, std::resetiosflags, std::quoted and the money and time helpers. A manipulator is inserted into a stream with << (or extracted with >>) like a value, and it changes how later items are formatted.

The argument-free manipulators (std::hex, std::fixed, std::left, std::boolalpha, ...) live in <ios> and <iostream>. Together they are the stream way of doing what printf flags and format specifications do.

C++
#include <iomanip>
#include <iostream>

std::cout << std::setw(6) << std::setfill('0') << 42;   // 000042

Try it

C++ A neat table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Precision, bases, internal fill and quoted

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
/* unspecified */ setw(int n);
/* unspecified */ setfill(char c);
/* unspecified */ setprecision(int n);
/* unspecified */ setbase(int base);                 // 8, 10 or 16
/* unspecified */ setiosflags(std::ios_base::fmtflags mask);
/* unspecified */ resetiosflags(std::ios_base::fmtflags mask);
/* unspecified */ quoted(const std::string& s, char delim = '"', char escape = '\\');
/* unspecified */ get_money / put_money / get_time / put_time
Name Description
n (setw) Minimum width of the next formatted item.
c (setfill) Padding character used when the item is shorter than the width. Default space.
n (setprecision) Digits after the point with fixed / scientific; significant digits otherwise.
base 8, 10 or 16; any other value resets to 0, which means "no base conversion".
mask Flags such as `std::ios::hex

Returns: an object of an unspecified type that you pass to << or >>. Do not store it; use it only inline.

Errors / undefined behavior: none for ordinary use. A negative width is treated as zero; very large precisions are accepted but only digits that exist are printed.

Usage notes

  • One-shot versus sticky. setw affects only the next formatted item and is reset afterwards. setfill, setprecision, setbase, fixed, hex, left and the rest stay until you change them. Restore the state after changing it, especially in functions that use a caller's stream.
  • Strings are not truncated by setw. Items wider than the width print in full. There is no stream manipulator to truncate; use substr or std::format with a precision.
  • Alignment. std::left, std::right and std::internal (sign or prefix on the left, digits on the right) choose where the fill goes. The default is right.
  • setprecision meaning changes with fixed. Without fixed or scientific, it counts significant digits and trailing zeros are dropped; with fixed it counts decimals and keeps trailing zeros. Reset with unsetf(std::ios::floatfield) or std::defaultfloat.
  • std::put_time(&tm, "%Y-%m-%d") formats a std::tm; its output depends on the current time and the C locale, so examples that print it are not deterministic.
  • setw with input. cin >> std::setw(n) >> charArray limits how many characters are read into a C array, a classic safety measure.
  • Alternative. The <format> library does all of this in one string, e.g. std::format("{:>10.2f}", x), with no state left behind.