Skip to main content

ios functions and manipulators in stream I/O

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.

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

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
cout.width(8);
cout.fill('0');
cout.precision(2);
cout.setf(ios::fixed);
cout << 3.5;          // 00003.50

Difference from manipulators

ios functionsManipulators
Member functions called with the stream object (cout.width(5))Used directly in the << / >> chain (cout << setw(5))
Need separate statementsCan be chained in one statement
width() affects the next item only; others staysetw 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 easilyUser-defined manipulators can be written

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 8 questions →