0
0
CppHow-ToBeginner · 3 min read

How to Format Output in C++: Syntax and Examples

In C++, you format output using std::cout along with manipulators like std::setw for width, std::setprecision for decimal places, and std::fixed for fixed-point notation. These tools help control how text and numbers appear on the screen.
📐

Syntax

Use std::cout to print output. Manipulators from <iomanip> help format the output:

  • std::setw(n): sets the width of the next output field to n characters.
  • std::setprecision(n): sets the number of significant digits for floating-point numbers by default, or decimal places if used with std::fixed.
  • std::fixed: forces floating-point numbers to be shown in fixed-point notation.
  • std::left and std::right: align output to left or right within the field.
cpp
#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::setw(10) << 123 << "\n";
    std::cout << std::fixed << std::setprecision(2) << 3.14159 << "\n";
    return 0;
}
Output
123 3.14
💻

Example

This example shows how to format numbers with width, precision, and alignment to create a neat table-like output.

cpp
#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::left << std::setw(10) << "Name" 
              << std::right << std::setw(8) << "Score" << "\n";
    std::cout << std::left << std::setw(10) << "Alice" 
              << std::right << std::setw(8) << 95 << "\n";
    std::cout << std::left << std::setw(10) << "Bob" 
              << std::right << std::setw(8) << 87 << "\n";
    std::cout << std::left << std::setw(10) << "Carol" 
              << std::right << std::setw(8) << 92 << "\n";
    std::cout << std::fixed << std::setprecision(1);
    std::cout << "Average: " << (95 + 87 + 92) / 3.0 << "\n";
    return 0;
}
Output
Name Score Alice 95 Bob 87 Carol 92 Average: 91.3
⚠️

Common Pitfalls

Common mistakes when formatting output in C++ include:

  • Not including <iomanip> header when using manipulators like std::setw or std::setprecision.
  • Forgetting that std::setw applies only to the next output item, so you must use it before each item you want to format.
  • Using std::setprecision without std::fixed can lead to scientific notation instead of fixed decimal places.
  • Not resetting manipulators if you want different formatting later in the output.
cpp
#include <iostream>
#include <iomanip>

int main() {
    // Wrong: setw applies only to next output
    std::cout << std::setw(10) << 123 << 456 << "\n"; // 456 not formatted

    // Right: setw before each
    std::cout << std::setw(10) << 123 << std::setw(10) << 456 << "\n";

    // Without fixed, setprecision sets significant digits
    std::cout << std::setprecision(3) << 1234.56789 << "\n";

    // With fixed, shows fixed decimal places
    std::cout << std::fixed << std::setprecision(3) << 1234.56789 << "\n";

    return 0;
}
Output
123456 123 456 1.23e+03 1234.568
📊

Quick Reference

ManipulatorPurposeExample
std::setw(n)Set width of next output fieldstd::cout << std::setw(10) << 42;
std::setprecision(n)Set decimal digits for floats when used with std::fixed, otherwise sets significant digitsstd::cout << std::setprecision(2) << 3.14159;
std::fixedUse fixed-point notationstd::cout << std::fixed << 3.14159;
std::leftLeft-align outputstd::cout << std::left << std::setw(10) << "Hi";
std::rightRight-align outputstd::cout << std::right << std::setw(10) << "Hi";

Key Takeaways

Use std::cout with manipulators like std::setw and std::setprecision to control output format.
Remember std::setw affects only the next output item, so apply it before each item you want formatted.
Include header to use formatting manipulators.
Use std::fixed with std::setprecision to show fixed decimal places instead of scientific notation.
Use std::left and std::right to align output within a field width.