Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

Format fields

The second type of formatting flags work in a group. Only one of these flags can be set at a time, like the buttons on old car radios you push one in, the rest pop out. Unfortunately this doesn t happen automatically, and you must pay attention to what flags you re setting so that you don t accidentally call the wrong setf( ) function. For example, there s a flag for each of the number bases: hexadecimal, decimal, and octal. Collectively, these flags are referred to as the ios::basefield. If the ios::dec flag is set and you call setf(ios::hex), you ll set the ios::hex flag, but you won t clear the ios::dec bit, resulting in undefined behavior. Instead, call the second form of setf( ) like this: setf(ios::hex, ios::basefield). This function first clears all the bits in the ios::basefield and then sets ios::hex. Thus, this form of setf( ) ensures that the other flags in the group pop out whenever you set one. The ios::hex manipulator does all this for you, automatically, so you don t need to concern yourself with the internal details of the implementation of this class or to even care that it s a set of binary flags. Later you ll see that there are manipulators to provide equivalent functionality in all the places you would use setf( ).

Here are the flag groups and their effects:




Format integral values in base 10 (decimal) (the default radix no prefix is visible).


Format integral values in base 16 (hexadecimal).


Format integral values in base 8 (octal).





Display floating-point numbers in scientific format. Precision field indicates number of digits after the decimal point.


Display floating-point numbers in fixed format. Precision field indicates number of digits after the decimal point.

automatic (Neither bit is set.)

Precision field indicates the total number of significant digits.





Left-align values; pad on the right with the fill character.


Right-align values. Pad on the left with the fill character. This is the default alignment.


Add fill characters after any leading sign or base indicator, but before the value. (In other words, the sign, if printed, is left-justified while the number is right-justified.)


Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire