-- Programming Language Books and Training

 C C++ Java

Books by K. N. King

Books by K. N. King
Short courses
Recommended books
Recommended links

C Programming: A Modern Approach

Preface (slightly abridged)

In computing, turning the obvious into the useful is a living
definition of the word "frustration."
— Alan Perlis

Let me get this off my chest right at the start: I've had a love/hate relationship with C for years. I love the ease with which I can write C programs. I love the development environments that come with many of today's C compilers. But I hate the ease with which I can make mistakes. I hate the attention to picky details that C programming often requires. And, above all, I hate the way many C programmers disparage other languages. Let's face it: C isn't the ultimate programming language. (C++ isn't, either.) It is, however, a language with which every software developer should become familiar. It has become, for better or for worse, the lingua franca of the computer world.

I first used C in 1975, when it was new and somewhat immature. I then lost touch with the language for some years. Once C was standardized, I decided to take another look at it. To my relief, I found that some of its worst flaws had been corrected during standardization. (Of course, there are enough left to keep life interesting!) I decided to write a book that would take a fresh look at C, while at the same time tapping into the collective wisdom that C programmers have accumulated over the past quarter of a century.


Here are some of the goals I've tried to accomplish in this book:

  • 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. I assume that the reader has a minimal amount of 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 reader frustration, I've tried to cover all the features of the Standard C language and library, including signals, setjmp/longjmp, variable-length argument lists, and international features. 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. I use 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 language 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 consistent style. Rather than dictating what this style should be, though, I usually describe a few possibilities and let the reader chose 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 avoid dependence on any particular machine, compiler, or operating system. Of course, with a language like C, it's impossible to skip machine details completely. When such discussions are unavoidable, I give examples for both 16-bit and 32-bit architectures. When examples depend on a particular operating system, I discuss both DOS and UNIX.
  • Use illustrations to clarify key concepts. I've tried to put in as many figures as I 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:

  • 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 C. I pay minimal attention to older versions of the language. There are just a few scattered references to Classic (K&R) C in the chapters, mostly in Q&A sections. Appendix C lists the major differences between Standard C and Classic 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. I treat C as a mature software engineering tool, emphasizing how to use it to cope with issues that arise during programming-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 most C books do, I postpone them until Chapter 20.
  • De-emphasize "manual optimization." Many books teach the reader to write nonobvious 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.
  • Emphasize compatibility with C++. I'll have more to say about this later.

Q&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:

  • Frequently asked questions. I've tried to answer questions that come up frequently in my own courses, in other books, and on newsgroups related to C.
  • Additional discussion and clarification of tricky issues. Although readers with experience in a variety of languages may be satisfied with a brief explanation and a couple of examples, readers with less experience need more.
  • Side issues that don't belong in the main flow. Some questions raise technical issues that won't be of interest to all readers.
  • Material too advanced or too esoteric to interest the average reader. Questions of this nature are marked with an asterisk (*). Curious readers with a fair bit of programming experience may wish to delve into these questions immediately; others should definitely skip them on a first reading. Warning: These questions often refer to topics covered in later chapters.
  • Common differences among C compilers. I discuss some frequently used (but nonstandard) features that are provided by DOS and UNIX compilers.

Some questions in Q&A sections relate directly to specific places in the chapter; these places are marked by a special icon to signal the reader that additional information is available.

Other Features

In addition to Q&A sections, I've included a number of useful features, many of which are marked with simple but distinctive icons.

  • Warnings alert readers to common pitfalls. C is famous for its traps; documenting them all is a hopeless--if not impossible--task. I've tried to pick out the pitfalls that are most common and/or most important.
  • Cross-references provide a hypertext-like ability to locate information. Although many of these are pointers to topics covered later in the book, some point to previous topics that the reader may wish to review. idiom
  • Idioms—code patterns frequently seen in C programs—are marked for quick reference. portability tip
  • Portability tips give hints for writing programs that are independent of a particular machine, compiler, or operating system.
  • Sidebars cover topics that aren't strictly part of C but that every knowledgeable C programmer should be aware of, including unsigned integers, the IEEE floating-point standard, and Unicode. (See "Source Code" at the bottom of this page for an example of a sidebar.)
  • Appendices provide valuable reference information.


Choosing illustrative programs isn't an easy job. If programs are too brief and artificial, readers won't get any sense of how the features are used in the real world. On the other hand, if a program is too realistic, its point can easily be lost in a forest of details. I've chosen a middle course, using small, simple examples to make concepts clear when they're first introduced, then gradually building up to complete programs. I haven't included programs of great length; it's been my experience that instructors don't have the time to cover them and students don't have the patience to read them. I don't ignore the issues that arise in the creation of large programs, though—Chapter 15 (Writing Large Programs) and Chapter 19 (Program Design) cover them in detail.

Source Code
Source code for all programs in this book is available via the World-Wide Web at Updates, corrections, and news about the book are also available through this Web page.

Coverage of C++

C Programming: A Modern Approach was designed from the beginning to be completely compatible with C++, so that readers won't develop habits they must unlearn later. It prepares readers for C++ in three ways:

  • By stressing modern design principles such as information hiding.
  • By scattering brief discussions of C++—each tagged with a special "C++" icon—throughout the text.
  • By providing a detailed overview of C++ in Chapter 19.

C++ is complex enough to warrant its own book. Coincidentally, I just happen to have one in preparation. For more information, feel free to contact me, or watch my Web page for news.


This book is designed as a primary text for a C course at the undergraduate level. Previous programming experience in a high-level language or assembler is helpful, but not necessary for a computer-literate reader (an "adept beginner," as my editor likes to put it).

Since the book is self-contained and usable for reference as well as learning, it makes an excellent companion text for a course in data structures, compiler design, operating systems, computer graphics, or other courses that use C for project work. It's also suitable for use as one of several books in a "survey of programming languages" course.

Thanks to its Q&A sections and emphasis on practical problems, the book will also appeal to readers who are enrolled in a training class or who are learning C by self-study.


The book is divided into four parts:

  • Basic Features of C. Chapters 1-10 cover enough of C to allow the reader to write single-file programs using arrays and functions.
  • Advanced Features of C. Chapters 11-20 build on the material in the earlier chapters. The topics become a little harder in these chapters, which provide in-depth coverage of pointers, strings, the preprocessor, structures, unions, enumerations, and low-level features of C. In addition, two chapters (15 and 19) offer guidance on program design.
  • The Standard C Library. Chapters 21-26 focus on the C library, a large collection of functions that come with every compiler. These chapters are most likely to be used as reference material, although portions are suitable for lectures.
  • Reference. Appendix A covers the complete syntax of C, with annotations to explain some of the more obscure points. Appendix B gives a complete list of C operators. Appendix C describes the differences between Standard C and Classic C. Appendix D is an alphabetical listing of all functions in the C library, with a thorough description of each. Appendix E lists the ASCII character set. An annotated bibliography points the reader toward other sources of information.

A full-blown course on C should cover Chapters 1-20 in sequence, with topics from Chapters 21-26 added as needed. A shorter course can omit the following topics without losing continuity: Section 9.6 (recursive functions), Section 12.4 (pointers and multidimensional arrays), Section 14.5 (miscellaneous directives), Section 17.7 (pointers to functions), Chapter 19 (program design), Section 20.2 (bit-fields in structures), and Section 20.3 (other low-level techniques).


Having a variety of good exercises is obviously essential for a textbook. I've provided over 300 exercises at a variety of skill levels. Some are brief drill questions. Although these exercises aren't the most exciting (in fact, they can be downright boring), I consider them essential for developing skill in C, in the same way that vocabulary drill is needed in a foreign-language text or math problems in an algebra text. In addition to drill questions, I've included a number of short-answer questions and programming exercises. Short-answer questions require more thought than drill questions, although answers are usually brief. Programming exercises ask the reader to write a short program or a piece of a larger program.

A few exercises have nonobvious answers (some individuals uncharitably call these "trick questions"—the nerve!). Since C programs often contain abundant examples of such code, I feel it's necessary to provide some practice. However, I'll play fair by marking these exercises with an asterisk (*). Be careful with a starred exercise: either pay close attention and think hard or skip it entirely.

Errors, Lack of (?)

I've taken great pains to ensure the accuracy of this book. Inevitably, however, any book of this size contains a few errors. If you spot one, please email me or write to me at the following address:

K. N. King
Department of Mathematics and Computer Science
Georgia State University
University Plaza
Atlanta, GA 30303-3083

I'd also appreciate hearing about which features you found especially helpful, which ones you could do without, and what you'd like to see added.

Copyright © 1996 W. W. Norton & Company, Inc. All rights reserved.


Copyright © 1999-2006 K. N. King. All rights reserved.