...

/

Format Specifier

Format Specifier

Since we're tackling text, it would only be appropriate to have tools which can help us format our our data.

Format specifiers enable you to adjust the input and output data explicitly.

ℹ️ I use manipulators as format specifiers
The format specifiers are available as manipulators and flags. I only present manipulators in this book because their functionality is quite similar and manipulators are more comfortable to use.

Press + to interact
#include <iostream>
int main(){
std::cout << std::endl;
int num{2011};
std::cout << num << "\n\n";
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << num << std::endl;
std::cout.setf(std::ios::dec, std::ios::basefield);
std::cout << num << std::endl;
std::cout << std::endl;
std::cout << std::hex << num << std::endl;
std::cout << std::dec << num << std::endl;
std::cout << std::endl;
}

The followings tables present the important format specifiers. The format specifiers are sticky except for the field width, which is reset after each application.

The manipulators without any arguments require the header <iostream>, and the manipulators with arguments require the header <iomanip>.

Manipulator Stream type Description
std::boolalpha input and output Displays the boolean as a word.
std::noboolalpha input and output Displays
...