---
title: "ios functions and manipulators in stream I/O"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:ios-functions-manipulators"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/ios-functions-manipulators
site: BCT Engineering
---

# ios functions and manipulators in stream I/O

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

- Asked in: 2074 Asoj, 3+2 marks
- Video: [Formatted Input/Output in C++ | ios functions| width, precision, fill, setf, unsetf |Learners Region](https://www.youtube.com/watch?v=U-nCh5aOXM4) — Learners Region
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/ios-functions-manipulators
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

What are different ios functions used in stream I/O? How are they different from manipulators?

## Short answer

Ios functions (width, precision, fill, setf) are member functions called on the stream object, needing separate statements and returning old format values. Manipulators (setw, setprecision, endl) are inserted in the << chain, often requiring <iomanip>, and return the stream for chaining.

## Answer

#### ios functions used in stream I/O
Functions of the `ios` class (and its derived classes) control formatting and stream state:

- `width(n)` – field width for next value
- `precision(n)` – digits of precision
- `fill(c)` – padding character
- `setf(flags)` / `unsetf(flags)` – set/clear format flags
- `flags()` – read/set all flags
- `eof()`, `fail()`, `bad()`, `good()` – test stream state
- `clear()` – reset error state
- `put()`, `get()`, `getline()`, `read()`, `write()` – character/block I/O

```cpp
cout.width(8);
cout.fill('0');
cout.precision(2);
cout.setf(ios::fixed);
cout << 3.5;          // 00003.50
```

#### Difference from manipulators

| ios functions | Manipulators |
|---|---|
| Member functions called with the stream object (`cout.width(5)`) | Used directly in the `<<` / `>>` chain (`cout << setw(5)`) |
| Need separate statements | Can be chained in one statement |
| `width()` affects the next item only; others stay | `setw` also affects next item only; others stay |
| Return a value (e.g. old width/precision) | Return the stream reference |
| Defined in `<iostream>` | Many need `<iomanip>` |
| Cannot be user defined easily | User-defined manipulators can be written |

## Check yourself

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

1. How is an ios function different in use from a manipulator?
   A. They are identical
   B. ios function is called on the stream object; manipulator is used in the << chain
   C. ios function is used in the chain; manipulator is called on the object
   D. Manipulators need separate statements
   Answer: B. cout.width(5) versus cout << setw(5). (Key point: Call style)

2. Which can be chained in one statement?
   A. Both equally
   B. Neither
   C. ios member functions
   D. Manipulators
   Answer: D. Manipulators return the stream reference and can be chained. (Key point: Chaining)

3. Which functions test the stream state?
   A. put(), get()
   B. width(), fill()
   C. eof(), fail(), bad(), good()
   D. setf(), unsetf()
   Answer: C. eof, fail, bad and good report the stream state. (Key point: State functions)

## Related in Object Oriented Programming

- [ios class functions and flags for formatted I/O](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aios-flags-formatted-io) — Object Oriented Programming, chapter 8, asked 2×

## Related in other subjects

- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
- [Stream gauging with calibration equation](https://www.bctengineer.com/question.md?key=semester_6%2Fhydro_power%3Astream-gauging-calibration-no-data) — Hydro Power, chapter 2
- [Methods of stream flow measurement](https://www.bctengineer.com/question.md?key=semester_6%2Fenergy_resources%3Aflow-measurement-methods-micro-hydro) — Energy Resources, chapter 5
- [RA: employee-projects with no part used](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Ara-emp-project-no-part) — Database Management System, chapter 3
