To control the output style of cout in C++, we use format manipulators. These manipulators are from the <iomanip> header and help adjust the display of numbers, alignment, decimal places, and more. This article quickly guides you through common output formatting techniques with simple examples.
1. Include Necessary Headers¶
Before using cout and format manipulators, include these headers:
#include <iostream> // Provides cout, cin, and other I/O streams
#include <iomanip> // Provides format manipulators (e.g., setw, setprecision)
using namespace std; // Avoid repeating "std::" prefixes
2. Integer Output: Control Number Base¶
By default, cout outputs integers in decimal. Use manipulators to switch to hexadecimal or octal:
| Manipulator | Purpose | Example (Output for 10) |
|---|---|---|
dec |
Decimal (default) | cout << dec << 10; → 10 |
hex |
Hexadecimal (lowercase) | cout << hex << 10; → a |
oct |
Octal | cout << oct << 10; → 12 |
Note: Once hex or oct is set, subsequent outputs retain that base until reset to dec:
cout << dec << 10 << endl; // Decimal: 10
cout << hex << 10 << endl; // Hexadecimal: a
cout << oct << 10 << endl; // Octal: 12
cout << dec << 10 << endl; // Reset to decimal: 10
3. Floating-Point Output: Control Decimal Places and Notation¶
Floating-point numbers (e.g., 3.1415926) can be formatted using fixed, scientific, and setprecision:
1. fixed: Fixed Decimal Places¶
Use fixed to specify n decimal places, paired with setprecision(n):
cout << fixed << setprecision(3) << 3.1415926 << endl; // Output: 3.142 (3 decimal places)
2. scientific: Scientific Notation¶
Use scientific to display numbers in scientific notation (e.g., 1.23e+04), also paired with setprecision(n):
cout << scientific << setprecision(3) << 12345.67 << endl; // Output: 1.235e+04
3. setprecision(n): Significant Digits Control¶
By default, setprecision(n) controls the number of significant digits (without fixed or scientific):
cout << setprecision(3) << 12345.67 << endl; // Output: 1.23e+04 (3 significant digits)
cout << fixed << setprecision(3) << 12345.67 << endl; // Output: 12345.670 (3 decimal places)
4. Alignment and Width Control¶
Use setw(n) for width, left/right for alignment, and setfill(c) for fill characters:
1. Set Width: setw(n)¶
setw(n) affects only the next output item, padding with spaces if needed:
cout << setw(10) << "Hello" << endl; // Output: " Hello" (width 10, right-aligned)
cout << setw(10) << "World" << endl; // Output: " World"
2. Alignment: left/right¶
left: Left-align content.right: Right-align (default).
cout << left << setw(10) << "C++" << endl; // Output: "C++ " (left-aligned, width 10)
cout << right << setw(10) << "Python" << endl; // Output: " Python" (right-aligned, width 10)
3. Fill Character: setfill(c)¶
Set a custom fill character (e.g., * or #), paired with setw(n):
cout << setfill('*') << setw(10) << "Test" << endl; // Output: "Test******" (width 10, filled with *)
5. Common Tips: endl vs \n¶
endl: Inserts a newline and flushes the output buffer (ensures immediate display).\n: Inserts a newline without flushing (delayed output, better for performance).
cout << "Hello" << endl; // Newline + flush
cout << "World" << '\n'; // Newline only (no flush)
Summary¶
Master C++ output formatting by using <iomanip> manipulators:
- Integers: dec/hex/oct for number bases.
- Floating-point: fixed/scientific/setprecision for decimal places or scientific notation.
- Alignment: setw(n) + left/right + setfill(c) for width and alignment.
Practice combining these techniques to create clean, well-formatted output!