C++ std::ios_base::width() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, width(n) sets the minimum number of characters that the next value will take when it is written. Shorter values are padded with the fill character, which is a space by default.

Without an argument, width() only tells you the current width. It is the member-function twin of the setw manipulator. Include <iostream> to use it.

This is a member function of std::ios_base. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call width on a stream. With a number it sets the width. Without one it returns the current width.

C++
std::cout.width(8);
std::cout << 42 << '\n';        // "      42"

std::streamsize w = std::cout.width();   // read the width
CallWhat it does
std::cout.width(8)The next output takes at least 8 characters.
std::cout.width()Returns the current width without changing it.
in.width(4); in >> buf;For input, reads at most 3 characters into a char array.

The width applies to one item only. After that item, the width goes back to 0.

Library declarations
C++
streamsize width() const;
streamsize width(streamsize new_width);

Parameters

ParameterDescription
new_widthOptional. The new width. If left out, width() only returns the current value.

Return Value

Return ValueDescription
std::streamsizeThe width that was set before the call. Without an argument, the current width.

C++ Examples

Example 1 — Pad a Number with width()

Only the first number gets the width. The second one does not.

C++
#include <iostream>

int main() {
    std::cout.width(8);
    std::cout << 42 << '\n';

    std::cout << 42 << '\n';
    return 0;
}

How It Works

  1. width(8) says that the next output should take at least eight characters.
  2. 42 has two characters, so six spaces are added in front. By default numbers are aligned to the right.
  3. After one output the width is reset to 0, so the second 42 is printed with no padding.

Example 2 — width() with a Fill Character

Combine width() with fill() and left to draw a dotted line.

C++
#include <iostream>

int main() {
    std::cout.fill('.');
    std::cout << std::left;

    std::cout.width(10);
    std::cout << "Name" << "Ari" << '\n';

    std::cout.width(10);
    std::cout << "Language" << "C++" << '\n';
    return 0;
}

How It Works

  1. fill('.') changes the padding character and left puts the padding after the text.
  2. width(10) makes Name take ten places, so six dots are added. Ari follows with no width.
  3. The width is set again before Language, because it was reset after the first line.

Example 3 — Limit How Much Is Read into a Char Array

For input, width(n) stops >> after n - 1 characters, which protects the array.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("abcdefg");
    char buf[4];

    in.width(4);
    in >> buf;
    std::cout << "first:  " << buf << '\n';

    in.width(4);
    in >> buf;
    std::cout << "second: " << buf << '\n';
    return 0;
}

How It Works

  1. buf holds four characters, three letters and the end marker.
  2. width(4) tells >> to read at most three characters, so only abc is taken, and the rest stays in the stream.
  3. The width was reset, so it is set again before the second read. That read gives def.

Frequently Asked Questions

width(n) sets the minimum number of characters for the next output. Shorter values are padded. Without an argument it returns the current width.
It is reset to 0 after each formatted output or input. Set it again before each value.
They do the same job. width() is a member call (cout.width(8)). setw() is a manipulator from <iomanip> that fits inside a chain (cout << setw(8) << x).
No. The width is a minimum. A value longer than the width is printed in full.
Use fill(), for example cout.fill('0') pads with zeros. The setfill manipulator does the same inside a chain.

Next: write()

Write a block of characters, with no formatting.

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

8 people found this page helpful