Home C++ Iostream Functions setfill() C++ setfill() Manipulator Definition In C++, setfill(c) sets the padding character that a stream uses when a value is shorter than its field width. The default padding is a space.
It is used together with setw. Typical uses are zero-padded numbers like 007, lines made of = signs, and dotted menus. Include <iomanip> to use it.
This is a manipulator with an argument . You write it inside a stream chain, like std::cout << setfill(...), and it changes how the stream treats the data. It lives in <iomanip>, so remember to include that header.
Syntax Put std::setfill(c) into the stream, then use std::setw for the field.
#include <iomanip>
std::cout << std::setfill('*') << std::setw(8) << 42; // ******42 Call What it does std::cout << std::setfill('*')Pads with stars from now on. std::cout << std::setfill('0') << std::setw(2) << 5Prints 05. std::cout << std::setfill(' ')Goes back to spaces.
Unlike setw, the fill character stays until you change it.
Library declarations /* unspecified */ setfill(char c); Parameters Parameter Description cRequired. The character used for padding, such as '*' or '0'.
Return Value Return Value Description Unspecified type A small helper object that sets the fill character when it is sent into a stream. You never name its type. Just write setfill(c) inside the chain.
Important
The fill character stays on the stream. If you forget to reset it, later padded output keeps using it, for example zeros or stars in your next table. Set it back with setfill(' '). Without setw the fill character is never used.
C++ Examples 1. Stars 2. A clock 3. A line Example 1 — Pad with Stars A fill character shows exactly how many places were added.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setfill('*') << std::setw(8) << 42 << '\n';
return 0;
} How It Works setfill('*') makes the padding character a star.setw(8) asks for eight places, and 42 uses two of them.The other six places are filled with stars, in front of the number. Example 2 — Print a Time Like 05:07 Zero padding with a width of two.
#include <iomanip>
#include <iostream>
int main() {
int hour = 5;
int minute = 7;
std::cout << std::setfill('0');
std::cout << std::setw(2) << hour << ':' << std::setw(2) << minute << '\n';
return 0;
} How It Works setfill('0') is set once, and it stays active for both numbers.setw(2) comes before each value, because the width works only once. 5 becomes 05 and 7 becomes 07.The colon is a normal character. It is printed as it is, without any padding. Example 3 — Draw a Line and Then Reset the Fill Print an empty string in a wide field to draw a line. Then go back to spaces.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setfill('=') << std::setw(20) << "" << '\n';
std::cout << std::setfill(' ') << std::setw(5) << 7 << '\n';
return 0;
} How It Works setw(20) gives a field of twenty places, and the empty string "" fills none of them.All twenty places are filled with the fill character =, which draws the line. setfill(' ') goes back to spaces, so the number 7 is padded with four spaces in the second line.Frequently Asked Questions What does setfill() do in C++? It sets the character used to pad a field when a value is shorter than the width from setw. The default is a space.
How do I print numbers with leading zeros? Use setfill('0') and setw(n) together. For example, setw(3) with 7 prints 007.
Does setfill stay on? Yes. Unlike setw, the fill character stays until you change it. Reset it with setfill(' ').
What is the member function version? cout.fill(c) sets the fill character and returns the old one.
Does setfill work without setw? No visible effect. The fill character is used only when a width creates padding.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion