Home C++ Iostream Functions unsetf() C++ std::ios_base::unsetf() Function 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 |.
std::cout.unsetf(std::ios::showpos);
std::cin.unsetf(std::ios::skipws);
out.unsetf(std::ios::showbase | std::ios::uppercase); Call What 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 void unsetf(fmtflags mask); Parameters Parameter Description maskRequired. The flag or flags to turn off, joined with |.
Return Value Return Value Description Nothing unsetf() returns void. The flags of the stream are changed.
Important
The unsetf() call turns flags off, but it does not set a replacement. After unsetf(std::ios::dec) the number base is not decimal and not anything else, so output may look wrong. To change a group such as the base, use setf(flag, mask) instead.
C++ Examples 1. Turn off showpos 2. Turn off showbase 3. Read spaces Example 1 — Stop Showing the Plus Sign Turn the flag on, print, turn it off, and print again.
#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 setf(std::ios::showpos) makes positive numbers print with a plus sign, so the first line is +7.unsetf(std::ios::showpos) turns the flag off again.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.
#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 The stream is switched to base 16. With showbase on, the number gets the 0x prefix and prints as 0xff. unsetf(std::ios::showbase) turns only the prefix flag off. The base stays hex.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.
#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 Normally in >> b skips all spaces in front of a character. That is the skipws flag. unsetf(std::ios::skipws) turns that flag off, so >> takes the very next character, whatever it is.a reads x and b reads the space, so the program prints a space.Frequently Asked Questions What does unsetf() do in C++? It turns off the format flags you pass in, such as showpos, showbase, uppercase, or skipws.
What is the difference between setf() and unsetf()? setf() turns flags on and unsetf() turns them off.
Is unsetf() the same as the no manipulators? Yes in effect. cout.unsetf(ios::showpos) does the same as cout << noshowpos, and cin.unsetf(ios::skipws) does the same as cin >> noskipws.
Can I use unsetf() to change the number base? 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.
What happens if the flag is already off? Nothing. unsetf() on a flag that is not set does no harm.
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.
7 people found this page helpful
Helpful Share Copy link Suggestion