C++ IOS Library - Flags



It is used to get/set format flags. The format flags of a stream affect the way data is interpreted in certain input functions and how these are written by certain output functions. See ios_base::fmtflags for the possible values of this function's argument and the interpretation of its return value.

The second form of this function sets the value for all the format flags of the stream, overwriting the existing values and clearing any flag not explicitly set in the argument. To access individual flags, see members setf and unsetf.


Following is the declaration for ios_base::flags function.

get (1)	fmtflags flags() const;
set (2)	fmtflags flags (fmtflags fmtfl);

The first form (1) returns the format flags currently selected in the stream.

The second form (2) sets new format flags for the stream, returning its former value.


fmtfl − Format flags to be used by the stream. ios_base::fmtflags is a bitmask type.

Return Value

The format flags selected in the stream before the call.


Basic guarantee − if an exception is thrown, the stream is in a valid state.

Data races

Concurrent access to the same stream object may cause data races.


In below example explains about ios_base::flags function.

#include <iostream>     

int main () {
   std::cout.flags ( std::ios::right | std::ios::hex | std::ios::showbase );
   std::cout.width (10);
   std::cout << 100 << '\n';
   return 0;

Let us compile and run the above program, this will produce the following result −