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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, setf() turns on format flags of a stream. Flags control how values are printed, for example std::ios::showpos adds a plus sign and std::ios::hex prints numbers in base 16.

The flags stay on until you turn them off with unsetf(). Most programs use manipulators like std::hex instead, but setf() does the same job as a member function. 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

Pass one flag, or several joined with |. The two-argument form also needs a mask.

C++
std::cout.setf(std::ios::showpos);
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout.setf(std::ios::fixed, std::ios::floatfield);
CallWhat it does
s.setf(std::ios::showpos)Turns on the plus sign for positive numbers.
s.setf(std::ios::hex, std::ios::basefield)Switches to base 16 and turns off the other bases.
auto old = s.setf(std::ios::fixed)Returns the old flags so you can restore them later with flags(old).

For flags from a group (base, float format, alignment), use the two-argument form. It removes the other flags of that group first.

Library declarations
C++
fmtflags setf(fmtflags flags);
fmtflags setf(fmtflags flags, fmtflags mask);

Parameters

ParameterDescription
flagsRequired. The flag or flags to turn on, joined with | when there are several.
maskOptional. The group of flags that is cleared first: std::ios::basefield, floatfield, or adjustfield.

Return Value

Return ValueDescription
fmtflagsThe flags that were set before the call. Keep them to restore the old state with flags(old).

C++ Examples

Example 1 — Show a Plus Sign with showpos

A single flag is enough for this simple format.

C++
#include <iostream>

int main() {
    std::cout.setf(std::ios::showpos);

    std::cout << 5 << ' ' << -5 << ' ' << 0 << '\n';
    return 0;
}

How It Works

  1. setf(std::ios::showpos) turns on the plus sign for non-negative numbers.
  2. 5 becomes +5. A negative number already has a sign, so -5 stays the same.
  3. Zero counts as non-negative, so it prints as +0. The flag stays on for all later output.

Example 2 — Switch the Number Base with a Mask

Set base 16, print, then set base 10 and print again.

C++
#include <iostream>

int main() {
    std::cout.setf(std::ios::hex, std::ios::basefield);
    std::cout << 255 << '\n';

    std::cout.setf(std::ios::dec, std::ios::basefield);
    std::cout << 255 << '\n';
    return 0;
}

How It Works

  1. The mask std::ios::basefield first clears the decimal, octal, and hex flags, and then hex is turned on.
  2. 255 in base 16 is ff, so that is printed.
  3. The second call clears the hex flag and turns on dec, so 255 is printed in normal decimal form.

Example 3 — Save and Restore the Old Flags

setf() returns the old flags. Pass them to flags() to go back.

C++
#include <iostream>

int main() {
    std::ios::fmtflags old = std::cout.setf(std::ios::showpos);

    std::cout << 3 << '\n';

    std::cout.flags(old);
    std::cout << 3 << '\n';
    return 0;
}

How It Works

  1. setf(std::ios::showpos) turns the flag on and returns the flags from before.
  2. While the flag is on, 3 prints as +3.
  3. flags(old) puts all flags back as they were, so the last 3 prints without a plus sign.

Frequently Asked Questions

It turns on format flags of a stream, such as showpos, hex, fixed, or uppercase, and returns the flags that were set before.
The decimal flag is still on, so the stream stays in base 10. Use setf(ios::hex, ios::basefield), which clears the other bases first.
They do the same job. cout.setf(ios::hex, ios::basefield) and cout << hex both switch to base 16. Manipulators are shorter and easier to read.
Use unsetf(flag). For a group such as the base, setting another member of the group with the mask form is enough.
Save the return value of setf(), or call flags() before changing anything, and pass it back to flags() later.

Next: sync()

Synchronize the input buffer with its source.

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

10 people found this page helpful