C++ setw() Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

Definition

In C++, setw(n) sets the minimum number of characters that the next item takes when it is printed. If the item is shorter, the rest is filled with the fill character, a space by default.

It is the main tool for printing tables with straight columns. The width applies to one item only, so you write it again before each value. Include <iomanip> to use it.

This is a manipulator with an argument. You write it inside a stream chain, like std::cout << setw(...), and it changes how the stream treats the data. It lives in <iomanip>, so remember to include that header.

Syntax

Put std::setw(n) into the stream right before the item you want to widen.

C++
#include <iomanip>

std::cout << std::setw(6) << 42;        //     42
std::cout << std::left << std::setw(6) << "ab" << '|';   // ab    |
CallWhat it does
std::cout << std::setw(6) << 42Prints 42 in a field of six places: four spaces and then the number.
std::cout << std::left << std::setw(6) << "ab"Prints ab followed by four spaces.
std::cin >> std::setw(4) >> bufReads at most 3 characters into a char array.

setw does not cut long values. A value that is wider than the field is printed in full.

Library declarations
C++
/* unspecified */ setw(int n);

Parameters

ParameterDescription
nRequired. The minimum number of characters of the next item. A value of 0 means no padding.

Return Value

Return ValueDescription
Unspecified typeA small helper object that sets the field width when it is sent into a stream. You never name its type. Just write setw(n) inside the chain.

C++ Examples

Example 1 — Pad a Number with setw

The width is only used for the next item.

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

int main() {
    std::cout << "[" << std::setw(6) << 42 << "]\n";
    std::cout << "[" << 42 << "]\n";
    return 0;
}

How It Works

  1. setw(6) makes the next item take six places. 42 has two digits, so four spaces are added in front.
  2. The brackets show the edges of the field.
  3. The second 42 is printed without setw, so it takes only its own two places. The width was used up by the first item.

Example 2 — Print a Table with Straight Columns

Set the width again before every item.

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

int main() {
    std::cout << std::left << std::setw(8) << "Item"
              << std::right << std::setw(5) << "Qty" << '\n';
    std::cout << std::left << std::setw(8) << "Apple"
              << std::right << std::setw(5) << 3 << '\n';
    std::cout << std::left << std::setw(8) << "Banana"
              << std::right << std::setw(5) << 12 << '\n';
    return 0;
}

How It Works

  1. The first column is eight places wide and left-aligned. The second is five places wide and right-aligned.
  2. Every row has the same widths, so the columns line up, however long the names are.
  3. setw is repeated for every item. Writing it only once at the top would not work.

Example 3 — Why setw Seems to Stop Working

Compare one setw for three numbers with one setw for each number.

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

int main() {
    std::cout << std::setw(5) << 1 << 2 << 3 << '\n';
    std::cout << std::setw(5) << 1 << std::setw(5) << 2 << '\n';
    return 0;
}

How It Works

  1. In the first line setw(5) belongs to 1 only. 2 and 3 follow with no padding, so you see 123.
  2. In the second line each number has its own setw(5), so each takes five places.
  3. If your columns do not line up, check that setw comes before every value.

Frequently Asked Questions

It sets the minimum field width for the next output item. Shorter items are padded with the fill character.
The width is reset to 0 after each item. Write setw again before each value.
#include <iomanip>. setw takes an argument, so it is defined there.
Put std::left into the stream. Then the padding is added after the value.
No. The width is a minimum. Longer text is printed in full and can break your columns.

Next: showbase

Print a prefix such as 0x for hex and 0 for octal.

showbase tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful