Index / 001 EN
The C Programming Language cover

Computer Science

Computer Science

The C Programming Language

Brian W. Kernighan; Dennis M. Ritchie

A concise classic introduction to C that teaches programming fundamentals through functions, types, pointers, memory, and portable systems code.

Difficulty Level
Intermediate
Academic Level
Undergraduate
C programmingprogramming languagespointersmemoryAlgorithmssystems programming

01 / Reading Guide

Reading Guide

Citation

Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd ed.). Prentice Hall.

Chapter Summary

The second edition is a compact book (288 pages in the publisher's listing, with the index beginning on page 263): a preface, an introduction, eight chapters and three appendices. Written by the designer of C (Ritchie) and a colleague from Bell Labs, it describes C as defined by the ANSI standard, which was being finalized as the book went to press; the earliest printings carry the words "Based on Draft-Proposed ANSI C" on the cover, later ones a red "ANSI C". The preface states the authors' aims: to keep the brevity of the first edition (1978), because C is not a big language and is not well served by a big book; to improve the explanation of central features such as pointers; and to teach mainly by reading, writing and revising complete, tested programs rather than by stating rules. Chapters contain numbered exercises.

Chapter 1: A Tutorial Introduction

A fast tour of the central part of the language, meant to get readers writing programs as quickly as possible: getting started, variables and arithmetic expressions, the for statement, symbolic constants, character input and output, arrays, functions, call by value, character arrays, and external variables and scope. The introduction is explicit that the tutorial assumes working knowledge of programming and does not explain computers, compilation or what an assignment means.

Chapter 2: Types, Operators, and Expressions

The more formal treatment begins: variable names, data types and sizes, constants, declarations, arithmetic, relational and logical operators, type conversions, increment and decrement, bitwise operators, assignment operators, conditional expressions, and precedence and order of evaluation.

Chapter 3: Control Flow

Statements and blocks, if-else and else-if chains, switch, while and for loops, do-while, break and continue, and goto and labels. The publisher's sample of this chapter shows the style: short, complete functions (a binary search, conversion between numbers and strings) followed by exercises asking the reader to rewrite or extend them.

Chapter 4: Functions and Program Structure

Functions and how programs are organized across them: functions returning non-integers, external variables, scope rules, header files, static and register variables, block structure, initialization, recursion, and the C preprocessor. According to the introduction, the chapter also covers programs split across multiple source files.

Chapter 5: Pointers and Arrays

The longest of the language chapters and the one the preface singles out for improved exposition. It covers pointers and addresses, pointers as function arguments, the relationship between pointers and arrays, address arithmetic, character pointers, arrays of pointers and pointers to pointers, multi-dimensional arrays, command-line arguments, pointers to functions, and complicated declarations, the last illustrated (per the preface) by programs that translate declarations into words and back.

Chapter 6: Structures

Structures and their use with functions, arrays of structures, pointers to structures, self-referential structures (the basis of linked data structures), a table-lookup example, typedef, unions and bit-fields.

Chapter 7: Input and Output

The standard library as the portable interface to the operating system: standard input and output, formatted output with printf, variable-length argument lists, formatted input with scanf, file access, error handling with stderr and exit, line input and output, and miscellaneous functions.

Chapter 8: The UNIX System Interface

How C programs use UNIX system calls directly: file descriptors, low-level read and write, open, creat, close and unlink, and random access with lseek. Three extended examples follow: an implementation of fopen and getc, a program that lists directories, and a storage allocator. The introduction notes that, although partly UNIX-specific, the chapter also shows how one version of the standard library can be implemented.

Appendices A–C: Reference Manual; Standard Library; Summary of Changes

Appendix A is a reference manual that conveys the essentials of the ANSI standard in a smaller space for programmers rather than compiler writers, ending with a grammar. Appendix B summarizes the standard library header by header, from stdio.h to limits.h and float.h. Appendix C lists the changes from the original language of the first edition.

Key Concepts

Chapters 1–3: The core language

  • Function prototypes: the ANSI form of declaration that lists argument types, which the introduction calls the most important change for most programmers.
  • Type conversions: the rules by which values of different types are combined in expressions.
  • Bitwise operators: operators that act on the individual bits of integers.
  • Structured control flow: if-else, switch, while, for, do-while, break and continue.

Chapters 4–6: Program structure, pointers and structures

  • Scope and external variables: which parts of a program can see a name, including variables shared across source files.
  • The preprocessor: macro substitution, file inclusion and conditional compilation before compilation proper.
  • Pointer arithmetic: computing addresses within arrays, and the close relationship between pointers and arrays.
  • Self-referential structures: structures containing pointers to structures of the same type, used to build lists and trees.

Chapters 7–8: Libraries and the system

  • Standard library: the ANSI-defined set of functions and headers that makes input, output and other system access portable.
  • System calls: low-level operations such as read, write and lseek provided by the operating system.
  • Storage allocation: managing memory explicitly, illustrated by a storage allocator.

Critical Analysis

Strengths

  • Brevity and precision. The authors set out to keep the book short because the language is small, and the whole language, standard library summary and reference manual fit in under 300 pages. Bjarne Stroustrup, quoted by the publisher, praises the deceptively light style of the tutorial chapters and the crispness of the reference manual.
  • Learning from complete programs. The preface stresses complete, real programs tested directly from the text, and exercises that ask readers to modify them; the authors also aim to illustrate useful algorithms and principles of good style and sound design. The example programs are available from the publisher as a download, one directory per chapter.
  • Written by the language's designer. The reference manual and the explanation of the standard come from the people who defined C, and the introduction is frank about the language's limits, including operators with the wrong precedence and parts of the syntax that could be better.
  • Bridge to systems programming. Chapter 8 shows how the library sits on top of system calls, ending with a storage allocator, which connects the language to how operating systems and libraries work.

Limitations

  • Not a first programming book. The preface says plainly that the book is not an introductory programming manual and assumes familiarity with variables, assignment, loops and functions; novices can follow it, but help from someone more experienced is advised.
  • Describes 1988 ANSI C. The book describes C as the ANSI standard defined it in 1988. Later revisions of the C standard are not covered, so readers must learn newer features and current library practice elsewhere.
  • Language, not data structures. The introduction states that the book is not a reference on data structures and algorithms; where a choice had to be made, the authors concentrated on the language.
  • Errata and printings. Ritchie maintained a list of known errors in the published version, many corrected in later printings; readers of older copies should consult it.

Where It Fits in the Course

Most of the other books in this introductory programming course are designed for people who have never programmed. Matthes's Python Crash Course teaches core Python with short exercises and then three projects; Felleisen and colleagues' How to Design Programs teaches a systematic design method in teaching languages that grow with the student; Downey's Think Python and Guttag's Introduction to Computation and Programming Using Python introduce computer science through Python. Kernighan and Ritchie assume that kind of first course and take the reader to a lower level: memory, addresses, pointers and the operating system interface that higher-level languages hide.

Abelson and Sussman's Structure and Interpretation of Computer Programs goes the opposite direction, toward abstraction, higher-order procedures and interpreters. Reading both shows two ends of programming: building abstractions up from a small language, and managing the machine directly. A student who has finished one of the Python books is well placed to read this book next, perhaps alongside a course on computer organization or operating systems.

How to Use This Book

The book suits students who can already program in some language and want to learn C properly, especially those heading to systems programming, embedded work or operating systems courses.

  • Read Chapter 1 at the keyboard. Type in and run every example; the authors' view is that the way to learn a language is to write programs in it.
  • Core: Chapters 2–6 in order. Spend the most time on Chapter 5; work through pointers and arrays with small test programs and drawings of memory before moving on to Chapter 6.
  • Library and system: Chapter 7 is essential for practical programs. Chapter 8 is optional for a first pass but rewarding for students taking systems courses; the storage allocator is worth studying line by line.
  • Reference: keep Appendices A and B for lookup rather than straight reading.

Do the exercises: they are short and closely tied to the examples, and many extend a program from the text. Compile with a modern compiler's warnings enabled, and use the publisher's downloadable example programs and Ritchie's errata list to check the text. Supplement the book with a current reference on the later C standards.