C Crash Course A Fast-Paced Introduction by Josh Lospinoso
IOSTRE AMS, PRINTF, AND INPUT OUTPUT PEDAGOGY People have really strong opinions about which standard output method to
teach C++ newcomers. One option is
printf
, which has a lineage that traces
back to C. Another option is
cout
, which is part of the C++ standard library’s
iostream
library. This book teaches both:
printf
in Part I and
cout
in Part II.
Here’s why.
This book builds your C++ knowledge brick by brick. Each chapter is
designed sequentially so you don’t need a leap of faith to understand code
examples. More or less, you’ll know exactly what every line does. Because
printf
is fairly primitive, you’ll have enough knowledge by Chapter 3 to
know exactly how it works.
In contrast,
cout
involves a whole lot of C++ concepts, and you won’t
have sufficient background to understand how it works until the end of Part I.
(What’s a stream buffer? What’s
operator<<
? What’s a method? How does
flush()
work? Wait,
cout
flushes automatically in the destructor? What’s a
destructor? What’s
setf
? Actually, what’s a format flag? A
BitmaskType
? Oh
my, what’s a manipulator? And so on.)
Of course,
printf
has issues, and once you’ve learned
cout
, you should
prefer it. With
printf
you can easily introduce mismatches between format
specifiers and arguments, and this can cause strange behavior, program
crashes, and even security vulnerabilities. Using
cout
means you don’t need
format strings, so you don’t need to remember format specifiers. You’ll never
get mismatches between format strings and arguments. Iostreams are also
extensible , meaning you can integrate input and output functionality into your
own types.
This book teaches modern C++ directly, but on this particular topic it
compromises a bit of modernist dogma in exchange for a deliberate, linear
approach. As an ancillary benefit, you’ll be prepared to encounter
printf
specifiers, which is likely to happen at some point in your programming career.
Most languages, such as C, Python, Java, and Ruby, have facilities for
printf
specifiers, and there are analogs in C#, JavaScript, and other languages.