K.N.KING
C PROGRAMMIN
A Modern Approach SECOND EDITIONK.N.KING
SECOND EDITION
‘and I look forward to using it in
future courses.”
— Karen Reid, Senior Lecturer,
Department of Computer Science,
University of TorontoPREFACE
In computing, turning the obvious into the uset
Is a living definition of the word “frustration.”
In the years since the first edition of C Programming: A Modern Approach was
published, a host of new C-based languages have sprung up—Iava and C# foremost
among them—and related languages such as C++ and Perl have achieved greater
prominence. Still, C remains as popular as ever, plugging away in the background,
quietly powering much of the world’s software. It remains the lingua franca of the
‘computer universe, as it was in 1996.
But even C must change with the times. The need for a new edition of C Pro-
gramming: A Modern Approach became apparent when the C99 standard was pub-
lished. Moreover, the first edition, with its references to DOS and 16-bit processors,
‘was becoming dated. The second edition is fully up-to-date and has been improved
in many other ways as well.
What’s New in the Second Edition
Here's a list of new features and improvements in the second edition:
= Complete coverage of both the C89 standard and the C99 standard, The big-
gest difference between the first and second editions is coverage of the C99 stan-
dard, My goal was to cover every significant difference between C89 and C99,
including all the language features and library functions added in C99, Each
C99 change is clearly marked, either with “C99” in the heading of a section or—
in the case of shorter discussions—with a special icon in the left margin, I did
this partly to draw attention to the changes and partly so that readers who aren’t
interested in C99 or don’t have access to a C99 compiler will know what to skip.
Many of the C99 additions are of interest only to a specialized audience, but
some of the new features will be of use to nearly all C programmers.
xxixxii
Preface
Includes a quick reference to all C89 and C99 library functions. Appendix
D im the first edition described all C89 standard library functions. In this edi-
tion, the appendix covers all C89 and C99 library functions.
Expanded coverage of GCC. In the years since the first edition, use of GCC
(originally the GNU C Compiler, now the GNU Compiler Collection) has
spread. GCC has some significant advantages, including high quality, low (i.c.,
no) cost, and portability across a variety of hardware and software platforms.
In recognition of its growing importance, I’ve included more information about
GCC in this edition, including discussions of how to use it as well as common
GCC error messages and warnings.
New coverage of abstract data types. In the first edition, a significant portion
of Chapter 19 was devoted to C++. This material seems less relevant today,
since students may already have learned C++, Java, or C# before reading this
book. In this edition, coverage of C++ has been replaced by a discussion of
how to set up abstract data types in C,
Expanded coverage of international features. Chapter 25, which is devoted
to C’s international features, is now much longer and more detailed. Informa-
tion about the Unicode/UCS character set and its encodings is a highlight of
the expanded coverage.
Updated to reflect today’s CPUs and operating systems. When I wrote the
first edition, 16-bit architectures and the DOS operating system were still rele-
vant to many readers, but such is not the case today. I’ve updated the discus-
sion to focus more on 32-bit and 64-bit architectures. The rise of Linux and
other versions of UNIX has dictated a stronger focus on that family of operat-
ing systems, although aspects of Windows and the Mac OS operating system
that affect C programmers are mentioned as well.
More exercises and programming projects. The first edition of this book con-
tained 311 exercises. This edition has nearly 500 (498, to be exact), divided
into two groups: exercises and programming projects.
Solutions to selected exercises and programming projects. The most frequent
request I received from readers of the first edition was to provide answers to
the exercises. In response to this request, I’ve put the answers to roughly one-
third of the exercises and programming projects on the web at Anking.com/
books/e2. This feature is particularly useful for readers who aren’t enrolled in
a college course and need a way to check their work. Exercises and projects
for which answers are provided are marked with a @ icon (the “W" stands for
“answer available on the Web”).
Password-protected instructor website. For this edition, I've built a new in-
structor resource site (accessible through knking.com/books/c2) containing
solutions to the remaining exercises and projects, plus PowerPoint presenta-
tions for most chapters. Faculty may contact me at [email protected] for a
password, Please use your campus email address and include a link to your
department's website so that I can verify your identity.Preface — xxiii
I've also taken the opportunity to improve wording and explanations through-
out the book. The changes are extensive and painstaking: every sentence has been
checked and—if necessary—rewritten.
Although much has changed in this edition, I've tried to retain the original
chapter and section numbering as much as possible. Only one chapter (the last one)
is entirely new, but many chapters have additional sections. In a few cases, existing
sections have been renumbered. One appendix (C syntax) has been dropped, but a
new appendix that compares C99 with C89 has been added,
Goals
The goals of this edition remain the same as those of the first edition:
= Be clear, readable, and possibly even entertaining. Many C books are too
concise for the average reader. Others are badly written or just plain dull. I've
tried to give clear, thorough explanations, leavened with enough humor to hold
the reader's interest.
= Be accessible to a broad range of readers, | assume that the reader has at least
a little previous programming experience, but I don’t assume knowledge of a
particular language. I've tried to keep jargon to a minimum and to define the
terms that I use, I've also attempted to separate advanced material from more
elementary topics, so that the beginner won't get discouraged,
= Be authoritative without being pedantic. To avoid arbitrarily deciding what to
include and what not to include, I've tried to cover all the features of the C lan-
guage and library. At the same time, I've tried to avoid burdening the reader
with unnecessary detail.
= Be organized for easy learning. My experience in teaching C underscores the
importance of presenting the features of C gradually, Tuse a spiral approach, in
which difficult topics are introduced briefly, then revisited one or more times
later in the book with details added each time. Pacing is deliberate, with each
chapter building gradually on what has come before. For most students, this is,
probably the best approach: it avoids the extremes of boredom on the one hand,
or “information overload” on the other.
= Motivate language features. Instead of just describing each feature of the lan-
guage and giving a few simple examples of how the feature is used, I’ve tried to
motivate each feature and discuss how it’s used in practical situations.
= Emphasize style. It's important for every C programmer to develop a consis-
tent style. Rather than dictating what this style should be, though, T usually
describe a few possibilities and let the reader choose the one that’s most
appealing. Knowing alternative styles is a big help when reading other people’s
programs (which programmers often spend a great deal of time doing),
= Avoid dependence on a particular machine, compiler, or operating system.
Since C is available on such a wide variety of platforms, I've tried to avoidxxiv Preface
dependence on any particular machine, compiler, or operating system. All pro-
grams are designed to be portable to a wide variety of platforms.
= Use illustrations to clarify key concepts. I've tried to put in as many figures as
could, since I think these are crucial for understanding many aspects of C. In
particular, I’ve tried to “animate” algorithms whenever possible by showing
snapshots of data at different points in the computation.
What’s So Modern about A Modern Approach?
One of my most important goals has been to take a “modern approach” to C. Here
are some of the ways I’ve tried to achieve this goal:
m Put C in perspective. Instead of treating C as the only programming language
worth knowing, I treat it as one of many useful languages. I discuss what kind
of applications C is best suited for; I also show how to capitalize on C's
strengths while minimizing its weaknesses.
= Emphasize standard versions of C. 1 pay minimal attention to versions of the
Janguage prior to the C89 standard. There are just a few scattered references to
K&R C (the 1978 version of the language described in the first edition of Brian
Kernighan and Dennis Ritchie’s book, The C Programming Language). Appen-
dix C lists the major differences between C89 and K&R C.
= Debunk myths. Today's compilers are often at odds with commonly held
assumptions about C. I don’t hesitate to debunk some of the myths about C or
challenge beliefs that have long been part of the C folklore (for example, the
belief that pointer arithmetic is always faster than array subscripting). I've re-
examined the old conventions of C, keeping the ones that are still helpful.
= Emphasize software engineering. | treat C as a mature software engineering
tool, emphasizing how to use it to cope with issues that arise during program-
ming-in-the-large. I stress making programs readable, maintainable, reliable,
and portable, and I put special emphasis on information hiding.
= Postpone C’s low-level features. These features, although handy for the kind
of systems programming originally done in C, are not as relevant now that C is
used for a great variety of applications. Instead of introducing them in the early
chapters, as many C books do, I postpone them until Chapter 20.
= De-emphasize “manual optimization.” Many books teach the reader to write
tricky code in order to gain small savings in program efficiency. With today’s
abundance of optimizing C compilers, these techniques are often no longer
necessary; in fact, they can result in programs that are less efficient.
QK&A Sections
Each chapter ends with a “Q&A section” —a series of questions and answers related
to material covered in the chapter. Topics addressed in these sections include: