Contents Up << >>
Why should I use <iostream.h> instead of the traditional <stdio.h>?
Increase type safety, reduce errors, improve performance, allow
extensibility, and provide subclassability.
Printf is arguably not broken, and scanf is perhaps livable despite
being error prone, however both are limited with respect to what C++ I/O can do.
C++ I/O (using "<<" and ">>") is, relative to C (using "
printf()" and "scanf()"):
- Type safe -- type of object being I/O'd is known statically by
the compiler, rather than via dynamically tested via "%" fields.
- Less error prone -- redundant info increases the chance of
errors. C++ I/O has no redundant "%" tokens to get right.
- Faster -- printf is an "interpreter" of a tiny language whose
constructs mainly include "%" fields; it uses these fields to select
the right formatting primitive at run-time. C++ I/O picks these routines
statically based on actual types of the args. This improves
performance.
- Extensible -- the C++ I/O mechanism allows new user-defined types
to be added without breaking existing code (imagine the chaos if
everyone was simultaneously adding new incompatible "%" fields to
printf and scanf?!).
- Subclassable -- ostream and istream (the C++
replacements for FILE*) are real classes, and hence subclassable.
This means you can have other user defined things that look and act like
streams, yet that do whatever strange and wonderful things you want. You
automatically get to use the zillions of lines of I/O code written by
users you don't even know, and they don't need to know about your
"extended stream" class.