---
title: "Features for formatting output"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:output-formatting-features"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/output-formatting-features
site: BCT Engineering
---

# Features for formatting output

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

- Asked in: 2070 Chaitra, 10 marks; 2069 Chaitra, 5 marks (asked 2 times)
- Video: [C++ Lesson 16.0 - Output Formatting](https://www.youtube.com/watch?v=N6q1sctkUCY) — S&T CAFE
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/output-formatting-features
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

List the features that are used in formatting the output. Explain each with example.

## Short answer

Output formatting uses three mechanisms: (1) ios member functions (width, precision, fill, setf); (2) format flags (left/right, dec/hex, fixed/scientific, showpos); (3) manipulators (setw, setfill, setprecision, endl). Example: cout << setw(8) << setfill('*') << 42;

## Answer

C++ provides three groups of features to format output:

1. `ios` class member functions
2. `ios` format flags (with `setf()` / `unsetf()`)
3. Manipulators (`<iomanip>` and `<iostream>`)

#### 1. ios member functions
- `width(n)` – minimum field width, applies to the next item only.
- `precision(n)` – number of significant digits (decimals with `fixed`).
- `fill(c)` – padding character for unused field width.
- `setf()`, `unsetf()` – set/clear flags.

```cpp
cout.width(8); cout.fill('#'); cout << 42;      // ######42
cout.precision(3); cout << 3.14159;             // 3.14
```

#### 2. Format flags
- Justification: `ios::left`, `ios::right`, `ios::internal`
- Base: `ios::dec`, `ios::oct`, `ios::hex`, with `ios::showbase`
- Floating point: `ios::fixed`, `ios::scientific`, `ios::showpoint`
- Others: `ios::showpos`, `ios::uppercase`, `ios::boolalpha`

```cpp
cout.setf(ios::showpos);
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout << 7.5;                                    // +7.50
cout.setf(ios::hex, ios::basefield);
cout << 255;                                    // ff
```

#### 3. Manipulators
- `setw(n)`, `setfill(c)`, `setprecision(n)`, `setiosflags(f)`, `resetiosflags(f)`
- `endl`, `ends`, `flush`, `dec`, `oct`, `hex`, `left`, `right`, `fixed`, `scientific`

```cpp
cout << setw(10) << setfill('*') << left << "Hi" << endl;   // Hi********
cout << hex << 255 << dec << " " << 255 << endl;             // ff 255
```

#### Complete example

```cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    cout << left  << setw(10) << "Item"
         << right << setw(8)  << "Price" << endl;
    cout << left  << setw(10) << "Book"
         << right << setw(8)  << fixed << setprecision(2) << 250.5 << endl;
    cout << left  << setw(10) << "Pen"
         << right << setw(8)  << 15.0 << endl;
    return 0;
}
```

Output:

```text
Item         Price
Book        250.50
Pen          15.00
```

#### Difference in use
- `width()` and `setw()` affect only the next value; the others stay until changed.
- Manipulators can be chained inside one `cout` statement; `ios` functions need separate statements.

## Check yourself

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

1. What are the three groups of output formatting features?
   A. printf, scanf, gets
   B. ios functions, ios format flags, manipulators
   C. Loops, arrays, pointers
   D. Classes, objects, templates
   Answer: B. Formatting uses ios member functions, format flags and manipulators. (Key point: Three groups)

2. Which formatting items affect only the next value?
   A. hex and dec
   B. width() and setw()
   C. fill() and setfill()
   D. precision() and setprecision()
   Answer: B. Width resets after one output; the others stay until changed. (Key point: Scope of settings)

3. What is a difference between manipulators and ios functions?
   A. ios functions can be chained, manipulators cannot
   B. No difference
   C. Manipulators can be chained in one cout statement; ios functions need separate statements
   D. Manipulators need separate statements
   Answer: C. Manipulators are used within the << chain; ios functions are separate calls. (Key point: Chaining)

4. What does cout << hex << 255 << dec << ' ' << 255 print?
   A. ff ff
   B. ff 255
   C. 255 255
   D. FF 255
   Answer: B. hex prints 255 as ff, then dec returns to decimal. (Key point: Example)

## Related in other subjects

- [IA features; 3 op-amp IA output voltage](https://www.bctengineer.com/question.md?key=semester_4%2Fadvanced_electronics%3Aia-features-3opamp-output) — Advanced Electronics, chapter 4
- [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×
- [CC amplifier Rin, Rout and why it is used](https://www.bctengineer.com/question.md?key=semester_3%2Felectronics_devices_and_circuit%3Acc-input-output-resistance-why-used) — Electronics Devices and Circuit, chapter 2
- [Why 8051 / its operational features](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Awhy-8051-features) — Embedded System, chapter 9, asked 2×
