Home C++ Iostream Functions setf() C++ std::ios_base::setf() Function 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.
std::cout.setf(std::ios::showpos);
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout.setf(std::ios::fixed, std::ios::floatfield); Call What 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 fmtflags setf(fmtflags flags);
fmtflags setf(fmtflags flags, fmtflags mask); Parameters Parameter Description 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 Value Description fmtflagsThe flags that were set before the call. Keep them to restore the old state with flags(old).
Important
With the one-argument form, setf(std::ios::hex) alone may do nothing, because the decimal flag is still on. Use setf(std::ios::hex, std::ios::basefield). Flags stay on, so restore the old flags when a function should not change the stream for the caller.
C++ Examples 1. Show plus sign 2. Number base 3. Restore flags Example 1 — Show a Plus Sign with showpos A single flag is enough for this simple format.
#include <iostream>
int main() {
std::cout.setf(std::ios::showpos);
std::cout << 5 << ' ' << -5 << ' ' << 0 << '\n';
return 0;
} How It Works setf(std::ios::showpos) turns on the plus sign for non-negative numbers.5 becomes +5. A negative number already has a sign, so -5 stays the same.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.
#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 The mask std::ios::basefield first clears the decimal, octal, and hex flags, and then hex is turned on. 255 in base 16 is ff, so that is printed. 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.
#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 setf(std::ios::showpos) turns the flag on and returns the flags from before.While the flag is on, 3 prints as +3. flags(old) puts all flags back as they were, so the last 3 prints without a plus sign.Frequently Asked Questions What does setf() do in C++? It turns on format flags of a stream, such as showpos, hex, fixed, or uppercase, and returns the flags that were set before.
Why does setf(ios::hex) not work alone? 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.
What is the difference between setf() and a manipulator? 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.
How do I turn a flag off? Use unsetf(flag). For a group such as the base, setting another member of the group with the mask form is enough.
How do I restore the old flags? Save the return value of setf(), or call flags() before changing anything, and pass it back to flags() later.
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.
10 people found this page helpful
Helpful Share Copy link Suggestion