C++ Format Specifiers
In this page:
#include <iomanip>
std::cout << std::fixed << std::setprecision(digits) << decimal_value;
std::cout << std::hex << integer_value; // or std::oct, std::dec
Floating-Point Formatting
The fixed stream manipulator forces floating-point numbers to print in standard decimal notation rather than switching to scientific notation for very large or small values.
Combine it with setprecision to control exactly how many digits appear after the decimal point, which is essential for anything like displaying money.
Example: Floating-Point Formatting
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.5
double price = 19.5;
// Print to the console with cout
std::cout << std::fixed << std::setprecision(2) << price << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Hexadecimal and Octal Formats
The hex and oct manipulators switch how subsequent integers print — hex shows them in base-16 and oct in base-8 — while dec switches back to the normal base-10 you'd expect.
These are especially useful when debugging low-level code, where seeing a value's hexadecimal form makes bit patterns and memory addresses much easier to read.
Example: Hexadecimal and Octal Formats
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 255
int value = 255;
// Print to the console with cout
std::cout << std::hex << value << std::endl;
// Print to the console with cout
std::cout << std::oct << value << std::endl;
std::cout << std::dec << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Output Format
By default C++ prints boolean values as the raw integers 1 and 0, which can be confusing in output meant for a human reader.
Applying boolalpha makes cout print the actual words true and false instead, and noboolalpha switches back to the numeric form if you need it again later.
Example: Boolean Output Format
#include <iostream>
int main() {
bool active = true;
std::cout << active << std::endl; // 1
std::cout << std::boolalpha << active << std::endl; // true
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Scientific Notation
scientific formats floating-point numbers using exponential notation, like 1.5e+08 instead of 150000000, which keeps very large or very small numbers compact and easy to compare at a glance.
This matters in scientific or engineering code where values can span many orders of magnitude in the same output.
Example: Scientific Notation
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare bigNumber and set it to 150000000.0
double bigNumber = 150000000.0;
// Print to the console with cout
std::cout << std::scientific << bigNumber << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base Prefix Formatting
showbase adds a visible prefix that identifies a number's base — 0x for hexadecimal and a leading 0 for octal — so a reader doesn't have to guess how to interpret the digits that follow.
It's most useful alongside hex or oct, since without it a hex value like ff could easily be mistaken for decimal.
Example: Base Prefix Formatting
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 255
int value = 255;
// Print to the console with cout
std::cout << std::showbase << std::hex << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Looking for
printfstyle specifiers like%dincout, when C++ streams use manipulators such ashexandfixedinstead. - Setting
fixedandsetprecisionand expecting them to reset, when they stay in effect for later output. - Printing a bool and expecting
true, when you needboolalphato show text instead of 1 or 0.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: