---
title: "Manipulators available in C++"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:manipulators"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/manipulators
site: BCT Engineering
---

# Manipulators available in C++

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2073 Shrawan, 4 marks; 2070 Asar, 1+3 marks (asked 2 times)
- Video: [Manipulators in c++ | Introduction | OOPs in C++ | Lec-61 | Bhanu Priya](https://www.youtube.com/watch?v=kZ0lFp_4tBo) — Education 4u
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/manipulators
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

What do you mean by manipulators? Explain different 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.

## Answer

#### 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
| Manipulator | Purpose |
|---|---|
| `endl` | Inserts newline and flushes the buffer |
| `ends` | Inserts null character |
| `flush` | Flushes the output buffer |
| `dec`, `oct`, `hex` | Sets 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`, `right` | Justification |
| `fixed`, `scientific` | Floating point format |
| `showpoint`, `showpos`, `showbase` | Display options |
| `ws` | Skips leading whitespace on input |

```cpp
#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:

```text
Pen...........25
3.14
0xff
```

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

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What are manipulators?
   A. Functions used with << or >> to change I/O format
   B. Compiler flags
   C. Classes that read files
   D. Keywords for loops
   Answer: A. Manipulators are used in the << / >> chain to alter format and can be chained. (Key point: Definition)

2. What does endl do?
   A. Inserts a null character
   B. Inserts a newline and flushes the buffer
   C. Only flushes
   D. Only inserts a tab
   Answer: B. endl inserts newline and flushes; ends inserts null. (Key point: endl)

3. Which header is needed for manipulators with arguments like setw()?
   A. <fstream>
   B. <cstdio>
   C. <iomanip>
   D. <string>
   Answer: C. Parameterised manipulators need <iomanip>. (Key point: Header)

## Related in other subjects

- [Consensus; highly available service](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aconsensus-high-availability) — Distributed System, chapter 6
