Skip to main content

Manipulators available in C++

Short answer

Manipulators are functions that change stream format when inserted/extracted. Common ones: endl (newline), setw(n), setfill(ch), setprecision(n), dec/oct/hex, left/right, fixed/scientific. They are chainable, e.g., cout << setw(10) << x.

What do you mean by manipulators? Explain different manipulators available in C++.

Manipulators

Manipulators are special functions or objects used with the insertion (<<) or extraction (>>) operator to change the format of I/O. They can be chained in one statement, e.g. cout << setw(8) << x << endl;. Those taking arguments need <iomanip>.

Common manipulators

ManipulatorPurpose
endlInserts newline and flushes the buffer
endsInserts null character
flushFlushes the output buffer
dec, oct, hexSets number base
setw(n)Sets field width
setfill(ch)Sets fill character
setprecision(n)Sets precision
setiosflags(f) / resetiosflags(f)Sets / clears format flags
left, rightJustification
fixed, scientificFloating point format
showpoint, showpos, showbaseDisplay options
wsSkips leading whitespace on input
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    cout << setfill('.') << left << setw(10) << "Pen"
         << right << setw(6) << 25 << endl;
    cout << fixed << setprecision(2) << 3.14159 << endl;
    cout << hex << showbase << 255 << endl;
    return 0;
}

Output:

Pen...........25
3.14
0xff

The fill character set by setfill('.') stays in effect until it is changed.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 8 questions →