---
title: "ios class functions and flags for formatted I/O"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:ios-flags-formatted-io"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/ios-flags-formatted-io
site: BCT Engineering
---

# ios class functions and flags for formatted I/O

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

- Asked in: 2073 Shrawan, 3 marks; 2072 Chaitra, 3 marks (asked 2 times)
- 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-flags-formatted-io
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

What are the different ios class functions and flags that are used for formatted I/O operation? Explain with usage.

## Short answer

ios functions width(n) sets next field width; precision(n) sets digits; fill(c) sets pad char; setf(flags) and unsetf(flags) manage flags. Format flags include left/right, dec/oct/hex, showbase, showpoint, showpos, fixed/scientific, uppercase, boolalpha.

## Answer

The `ios` class (base of all stream classes) provides **member functions** and **format flags** to control formatted I/O.

#### Member functions
| Function | Use |
|---|---|
| `width(n)` | Sets minimum field width for the next output only |
| `precision(n)` | Sets number of digits (decimal places with `fixed`) |
| `fill(ch)` | Sets padding character |
| `setf(flags)` | Turns on format flags |
| `unsetf(flags)` | Turns off format flags |

#### Format flags (used with `setf`)
| Flag | Effect |
|---|---|
| `ios::left`, `ios::right`, `ios::internal` | Justification in field |
| `ios::dec`, `ios::oct`, `ios::hex` | Number base |
| `ios::showbase` | Shows 0x or 0 prefix |
| `ios::showpoint` | Shows trailing decimal point/zeros |
| `ios::showpos` | Shows + for positive numbers |
| `ios::fixed`, `ios::scientific` | Floating point notation |
| `ios::uppercase` | Upper case in hex/exponent |
| `ios::boolalpha` | Prints true/false |

#### Usage

```cpp
#include <iostream>
using namespace std;
int main() {
    cout.fill('*');
    cout.width(10);
    cout.setf(ios::left);
    cout << "Ram" << endl;              // Ram*******

    cout.unsetf(ios::left);
    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(2);
    cout.width(10);
    cout << 3.14159 << endl;            // ******3.14

    cout.setf(ios::hex, ios::basefield);
    cout.setf(ios::showbase);
    cout << 255 << endl;                // 0xff
    return 0;
}
```

## Check yourself

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

1. How long does width(n) apply?
   A. Until changed
   B. Only to the next output item
   C. To the whole program
   D. To all integers
   Answer: B. width() affects only the next item output. (Key point: width)

2. What does cout.setf(ios::showpos) do?
   A. Shows hex base
   B. Left-justifies output
   C. Shows + for positive numbers
   D. Prints true/false
   Answer: C. showpos prints a + sign before positive numbers. (Key point: Flag usage)

3. With ios::fixed and precision(2), what does 3.14159 in width 10 and fill * print?
   A. ******3.14
   B. 3.141590
   C. 3.14*******
   D. *****3.141
   Answer: A. Fixed with 2 decimals gives 3.14, right-justified in width 10 with * padding. (Key point: Fixed precision)

## Related in Object Oriented Programming

- [ios functions and manipulators in stream I/O](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aios-functions-manipulators) — Object Oriented Programming, chapter 8
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5
- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5
- [Distance class relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adistance-relational-overload) — Object Oriented Programming, chapter 5
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9

## 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×
- [Formatted vs unformatted I/O](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aformatted-vs-unformatted-io) — Computer Programming, chapter 4, asked 4×
- [Source coders used in speech coding](https://www.bctengineer.com/question.md?key=semester_7%2Fwireless_communication%3Aspeech-source-coders) — Wireless Communication, chapter 6
- [Video rental shop class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avideo-rental-class-diagram) — Object Oriented Analysis and Design, chapter 2
