Home C++ Iostream Functions setw() C++ setw() Manipulator 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.
#include <iomanip>
std::cout << std::setw(6) << 42; // 42
std::cout << std::left << std::setw(6) << "ab" << '|'; // ab | Call What 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 /* unspecified */ setw(int n); Parameters Parameter Description nRequired. The minimum number of characters of the next item. A value of 0 means no padding.
Return Value Return Value Description Unspecified type A 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 1. Basic width 2. A table 3. Only once Example 1 — Pad a Number with setw The width is only used for the next item.
#include <iomanip>
#include <iostream>
int main() {
std::cout << "[" << std::setw(6) << 42 << "]\n";
std::cout << "[" << 42 << "]\n";
return 0;
} How It Works setw(6) makes the next item take six places. 42 has two digits, so four spaces are added in front.The brackets show the edges of the field. 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.
#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;
} Item Qty
Apple 3
Banana 12 How It Works The first column is eight places wide and left-aligned. The second is five places wide and right-aligned. Every row has the same widths, so the columns line up, however long the names are. 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.
#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 In the first line setw(5) belongs to 1 only. 2 and 3 follow with no padding, so you see 123. In the second line each number has its own setw(5), so each takes five places. If your columns do not line up, check that setw comes before every value. Frequently Asked Questions What does setw() do in C++? It sets the minimum field width for the next output item. Shorter items are padded with the fill character.
Why does setw only work for the first value? The width is reset to 0 after each item. Write setw again before each value.
Which header do I need for setw? #include <iomanip>. setw takes an argument, so it is defined there.
How do I left-align with setw? Put std::left into the stream. Then the padding is added after the value.
Does setw cut text that is too long? No. The width is a minimum. Longer text is printed in full and can break your columns.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion