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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, unsetf() turns off format flags of a stream. It is the opposite of setf(). A flag that is not set stays off, so calling it twice is harmless.

Use it when a flag such as showpos, showbase, or skipws should stop affecting the stream. 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 |.

C++
std::cout.unsetf(std::ios::showpos);
std::cin.unsetf(std::ios::skipws);
out.unsetf(std::ios::showbase | std::ios::uppercase);
CallWhat it does
s.unsetf(std::ios::showpos)Stops printing the plus sign.
s.unsetf(std::ios::skipws)Makes >> stop skipping leading spaces when it reads.
s.unsetf(std::ios::showbase | std::ios::uppercase)Turns off several flags at once.

unsetf() has the same effect as the "no" manipulators, for example noshowpos, noshowbase, and noskipws.

Library declarations
C++
void unsetf(fmtflags mask);

Parameters

ParameterDescription
maskRequired. The flag or flags to turn off, joined with |.

Return Value

Return ValueDescription
Nothingunsetf() returns void. The flags of the stream are changed.

C++ Examples

Example 1 — Stop Showing the Plus Sign

Turn the flag on, print, turn it off, and print again.

C++
#include <iostream>

int main() {
    std::cout.setf(std::ios::showpos);
    std::cout << 7 << '\n';

    std::cout.unsetf(std::ios::showpos);
    std::cout << 7 << '\n';
    return 0;
}

How It Works

  1. setf(std::ios::showpos) makes positive numbers print with a plus sign, so the first line is +7.
  2. unsetf(std::ios::showpos) turns the flag off again.
  3. The second 7 prints as a plain number, because the stream no longer has the flag.

Example 2 — Turn Off the 0x Prefix

In hex mode, showbase adds 0x to numbers. unsetf() removes it.

C++
#include <iostream>

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

    std::cout.unsetf(std::ios::showbase);
    std::cout << 255 << '\n';
    return 0;
}

How It Works

  1. The stream is switched to base 16. With showbase on, the number gets the 0x prefix and prints as 0xff.
  2. unsetf(std::ios::showbase) turns only the prefix flag off. The base stays hex.
  3. The same value now prints as ff, without the prefix.

Example 3 — Read a Space with unsetf(skipws)

By default >> skips spaces. Turn that off and the space becomes a normal character.

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

int main() {
    std::istringstream in("x y");
    in.unsetf(std::ios::skipws);

    char a = 0;
    char b = 0;
    in >> a >> b;

    std::cout << "a is " << a << '\n';
    std::cout << "b is " << (b == ' ' ? "a space" : "something else") << '\n';
    return 0;
}

How It Works

  1. Normally in >> b skips all spaces in front of a character. That is the skipws flag.
  2. unsetf(std::ios::skipws) turns that flag off, so >> takes the very next character, whatever it is.
  3. a reads x and b reads the space, so the program prints a space.

Frequently Asked Questions

It turns off the format flags you pass in, such as showpos, showbase, uppercase, or skipws.
setf() turns flags on and unsetf() turns them off.
Yes in effect. cout.unsetf(ios::showpos) does the same as cout << noshowpos, and cin.unsetf(ios::skipws) does the same as cin >> noskipws.
It is better to use setf(flag, ios::basefield) or a manipulator like hex or dec. unsetf() on the base flags leaves the stream with no base set.
Nothing. unsetf() on a flag that is not set does no harm.

Next: width()

Get or set the minimum width of the next output field.

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

7 people found this page helpful