C++ setfill() Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

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.

C++
#include <iomanip>

std::cout << std::setfill('*') << std::setw(8) << 42;   // ******42
CallWhat 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
C++
/* unspecified */ setfill(char c);

Parameters

ParameterDescription
cRequired. The character used for padding, such as '*' or '0'.

Return Value

Return ValueDescription
Unspecified typeA 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.

C++ Examples

Example 1 — Pad with Stars

A fill character shows exactly how many places were added.

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

int main() {
    std::cout << std::setfill('*') << std::setw(8) << 42 << '\n';
    return 0;
}

How It Works

  1. setfill('*') makes the padding character a star.
  2. setw(8) asks for eight places, and 42 uses two of them.
  3. 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.

C++
#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

  1. setfill('0') is set once, and it stays active for both numbers.
  2. setw(2) comes before each value, because the width works only once. 5 becomes 05 and 7 becomes 07.
  3. 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.

C++
#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

  1. setw(20) gives a field of twenty places, and the empty string "" fills none of them.
  2. All twenty places are filled with the fill character =, which draws the line.
  3. setfill(' ') goes back to spaces, so the number 7 is padded with four spaces in the second line.

Frequently Asked Questions

It sets the character used to pad a field when a value is shorter than the width from setw. The default is a space.
Use setfill('0') and setw(n) together. For example, setw(3) with 7 prints 007.
Yes. Unlike setw, the fill character stays until you change it. Reset it with setfill(' ').
cout.fill(c) sets the fill character and returns the old one.
No visible effect. The fill character is used only when a width creates padding.

Next: setprecision()

Set how many digits a floating-point number shows.

setprecision() 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.

12 people found this page helpful