About this book
The C Programming Language is a short book about a small language. Brian Kernighan and Dennis Ritchie published it with Prentice Hall in 1978. At the time C was a working tool inside Bell Telephone Laboratories, the research arm of the American telephone system. It had been built for one job: writing the Unix operating system and the programs that ran on it. Outside a few universities and Bell Labs itself, almost nobody had used it.
The book changed that. For about a decade it was the only real definition of the language. Programmers who wanted to know what C meant read this book, and compiler writers who wanted to build C for a new machine read its reference appendix. The informal name for that early dialect, K&R C, comes from the authors' initials. When the American National Standards Institute began work on an official standard in 1983, its committee started from this book.
A second edition appeared in 1988. It was revised to follow the draft ANSI standard, which was finished in 1989 and adopted by the international standards body in 1990. Most readers today meet the second edition. It is the one to get, and it is the one this study edition has mainly in mind. Where the two editions differ in ways that matter, we say so.
The question the book sets out to answer is practical. A working programmer, already familiar with the basic ideas of programming, wants to learn this language well enough to write real programs in it. What does that programmer need to know, in what order, and how can it be said briefly? The authors' answer is a book of about two hundred and seventy pages in the second edition, including a full reference manual and a summary of the standard library. Few language books before or since have been so short.
The intended reader is not a beginner at programming. The preface says plainly that the book assumes some acquaintance with basic programming ideas such as variables, assignment, loops and functions. It does not teach what a loop is. It teaches how C writes a loop and why C's loop looks the way it does. That choice of reader is one source of its compactness. Another is the language itself, which really is small. A third is the authors' discipline as writers, which we come to in a later chapter.
It is worth being clear about what kind of book this is. It is a tutorial followed by a reference. It is not a book about software engineering, about algorithms, or about the Unix system as a whole, although it touches on all three. It shows how to say things in C. The examples are small, complete programs and functions that do useful work: counting characters and words, converting temperatures, sorting lines, looking up words in a table, managing memory. Each one is chosen to teach a feature of the language and a habit of mind at the same time.
Why put it on a shelf next to Babbage, Turing, Shannon and von Neumann? Because those books describe what computation is, and this one shows how most of the world's computing has actually been written down for half a century. Operating system kernels, device drivers, network stacks, databases, the interpreters of languages like Python, and the firmware in cars and appliances are written mostly in C or in languages that borrowed its model. To understand that layer is to understand the floor that every other layer stands on.
The authors
Dennis Ritchie
Dennis MacAlistair Ritchie was born in 1941 in Bronxville, New York. His father worked at Bell Labs on switching systems. Ritchie studied physics and applied mathematics at Harvard, where he became interested in computers and did graduate work on the theory of recursive functions. He joined Bell Labs in 1967.
At Bell Labs he worked with Ken Thompson. The two had been part of the Multics project, a joint effort with MIT and General Electric to build a large time-sharing operating system. When Bell Labs withdrew from Multics in 1969, Thompson began writing a smaller system on a spare PDP-7 minicomputer. Ritchie joined him. That system became Unix.
The early Unix was written in assembly language, which ties a program to one kind of machine. Thompson had written a language called B, a stripped-down descendant of BCPL, the language Martin Richards had designed at Cambridge. B had no types: everything was a machine word. That worked on the PDP-7 but fitted poorly on the newer PDP-11, which addressed individual bytes and would later support floating point. Between about 1971 and 1973 Ritchie added types to B, along with structures and a richer treatment of pointers, and the result was C. In 1973 the Unix kernel was rewritten in C. That was an unusual step at the time: operating systems were expected to be written in assembly language for speed and control.
Ritchie and Thompson received the Turing Award, the highest honour in computer science, in 1983 for their work on operating systems and Unix in particular. Ritchie also wrote widely read papers on Unix and on the history of C, including a careful account of how the language developed, which remains the best short history of it. He died in October 2011. His death received far less public attention than that of Steve Jobs, a week earlier, although a good case can be made that more of the world's devices ran on his work.
Brian Kernighan
Brian Wilson Kernighan was born in 1942 in Toronto. He studied engineering physics at the University of Toronto and took a doctorate in electrical engineering at Princeton. He joined the Computing Science Research Center at Bell Labs, the same group that produced Unix. He did not design C, and he has always said so. His contribution to this book was above all as a writer and teacher, and the tutorial chapters are generally credited to him, while the reference manual comes from Ritchie.
Kernighan is one of the best technical writers the field has had, and that is the key fact about him for this book. Before it he had co-written, with P. J. Plauger, The Elements of Programming Style and Software Tools, both books that teach by taking real programs and making them clearer. He was one of the three authors of the AWK text-processing language, whose name comes from the initials of Aho, Weinberger and Kernighan. Later he wrote The Unix Programming Environment and The Practice of Programming, both with Rob Pike, and a book on the Go language. He moved to Princeton as a professor and has written several books explaining computing to general readers, along with a memoir of the Unix years.
One small fact connects him to every programmer who has ever learned a new language. The habit of making the first example print the words hello, world comes from his tutorial material at Bell Labs, and this book made it universal. Almost every language manual since has followed it.
How the partnership shows in the book
The book has the voice of two people who knew exactly what they were doing. Ritchie knew the language from the inside, every decision in it and the reason for it. Kernighan knew how to explain a thing once, correctly, with an example that shows it in use. The result reads as if a designer and a teacher sat at the same desk. When the text explains why a feature exists, you hear the designer. When it chooses which feature to show first, and with what program, you hear the teacher.
The shape of the book
The book has two parts that work very differently. The first is a tutorial that tells you how to use the language. The second is a reference manual that tells you exactly what the language is. Between them sit the chapters that go deeper into each feature.
The quick tour first
The book opens with a fast tour of the whole language. In a few dozen pages it shows variables, arithmetic, loops, constants, character input and output, arrays, functions, arguments and external variables. It does this through small working programs. The authors are open about the cost of this approach: some things are shown before they are fully explained, and some details are left out. They accept that cost because a reader who has seen the whole language in miniature can understand each later chapter in context. You learn the shape of the forest before you study each tree.
This is the most imitated feature of the book and the one that most rewards active reading. The tour is not meant to be read the way one reads a novel. It is meant to be typed in, compiled, run, changed and broken. The exercises after each section are short and mostly practical, such as modifying a program to do something slightly different.
The middle chapters
After the tour, the book goes through the language feature by feature, more slowly and more carefully. It deals with data types and expressions, then control flow, then functions and how a program is split into pieces, then pointers and arrays, then structures, then input and output through the standard library, and finally the interface to the Unix operating system. The order matters. Each chapter needs what came before it, and the hardest material, pointers, sits in the middle where the reader is ready for it.
The last of these chapters, on the Unix system interface, is different in kind. It shows how parts of the standard library can themselves be written in C on top of the operating system's own calls. A reader sees that the library functions used throughout the book are not magic. They are ordinary C code, and one could write them oneself. This is an important lesson about the whole language: almost nothing in C is hidden behind the curtain.
The reference manual
The appendix defines the language precisely. It is dense, terse and complete. In the first edition it was the nearest thing C had to a formal specification. In the second edition it was rewritten to follow the ANSI standard, and a second appendix summarises the standard library. A third short appendix lists what changed between the old language and the standard one.
The tutorial and the manual are meant to be used together. The tutorial gives intuition and habits. The manual settles questions. When the tutorial says something loosely, the manual says it exactly. A reader who never opens the appendix has read only half the book.
What moves through all of it
Three threads run through every part. The first is economy: every feature is shown in a short, complete program that does something useful. The second is the machine underneath: the book keeps reminding the reader what the computer is actually doing, where the bytes are, what happens to a value when it is converted, what a pointer actually holds. The third is the library: the book treats the standard library as part of the language one has to know, and repeatedly shows how its functions are built.
Idea one: a small language
The most important fact about C is how little it contains. It has a handful of basic data types: characters, integers of a few sizes, floating-point numbers of two or three sizes. It has a set of operators, a few control-flow statements, functions, arrays, pointers, structures and unions. That is nearly all. There are no built-in strings beyond arrays of characters with a zero at the end. There are no built-in lists, sets or tables. There is no input or output in the language itself. There is no automatic memory management. There is no notion of objects or classes, and no exceptions.
Everything else comes from libraries, which are written in C. Reading a line from a file, formatting a number, copying a block of memory, allocating space at run time: all of these are ordinary functions that a program calls. The language defines a standard set of them, but they are not part of its grammar.
Why smallness was a deliberate choice
Small does not mean primitive. The smallness was a design decision with clear reasons. A small language is easy to implement, so a compiler for a new machine could be written in months by a few people. That is one main reason C spread so fast: whenever a new computer appeared, a C compiler for it followed soon after, and with the compiler came the programs written in C. A small language is also easy to hold in your head. An experienced C programmer can know the whole language, which is not true of most languages designed since.
A good analogy is a well-stocked workshop compared with a factory. The factory has specialised machines that each do one job well. The workshop has a small set of general tools, and a skilled person can make almost anything with them, including new tools. C is the workshop. Its few features combine freely, and what is missing can be written.
What smallness costs
The cost is that the programmer must do things that other languages do automatically. Strings must be managed by hand. Memory must be requested and given back. Bounds are not checked. The language trusts the programmer and does not protect against mistakes. This is often summed up as trusting the programmer, and it is the source of both C's power and most of its dangers. A later chapter in this study edition returns to the dangers.
It is often said that C is a low-level language. That is half true. It is low in the sense that its operations map closely onto what the machine does. It is high in the sense that it is not tied to any one machine, has real functions, structured control flow and types. A better description is that C is a thin, portable layer over the kind of machine that most computers turned out to be: memory as a long array of bytes, values in registers, a stack for function calls.
Idea two: types, expressions and conversions
C programs are built from expressions: combinations of values and operators that produce a value. C has a large set of operators for a small language. Besides arithmetic and comparison, it has operators for working on individual bits, for increasing or decreasing a variable by one, for combining assignment with arithmetic, and a conditional operator that chooses between two values. Assignment is itself an expression with a value, which lets one write compact loops that read a value, test it and act on it in one line.
This compactness is part of the language's character. The book shows idioms such as copying one string into another in a loop whose body is empty because all the work happens in the test. Readers new to C often find such code cryptic. The book's view is that once the idioms are familiar they are easier to read than longer forms, because the eye takes in the whole action at once. Whether one agrees, a programmer who wants to read existing C code must know these idioms.
Types and their sizes
Every value in C has a type, and the type determines how many bytes it occupies and how its bits are interpreted. A character is usually one byte. An integer is the natural size for the machine, which differed between computers. Short and long integers give smaller and larger ranges. Integers can be signed or unsigned. Floating-point numbers come in single and double precision.
The language deliberately leaves the exact sizes partly open. An integer is guaranteed to be at least a certain size, but on one machine it may be sixteen bits and on another thirty-two. This lets C use whatever is fastest on each machine. It also means that a program which assumes a particular size may break when moved. Much of the craft of portable C lies in not making such assumptions.
Conversions
When an expression mixes types, C converts values according to fixed rules. Smaller integers are widened, integers become floating-point when combined with floating-point values, and so on. The book explains these rules carefully, because they are a common source of bugs. A classic example is mixing signed and unsigned integers, where a negative number can silently become a very large positive one. Another is integer division, where dividing five by nine gives zero, not a fraction, which is why a temperature conversion program can produce nothing but zeros if written carelessly. The book uses exactly that kind of example early on to make the point.
The lesson is that in C the type of a value is not decoration. It decides what the machine actually does. Programmers coming from languages where numbers just behave like numbers need to unlearn that assumption.
Control flow
C's control flow is the structured set that ALGOL and its descendants had made standard: if and else for choices, while and for loops, a do loop that tests at the bottom, a switch statement for choosing among many constant cases, break and continue for leaving or restarting a loop, and a goto statement that the book admits exists and advises using rarely. The for loop is worth singling out. It gathers the setup, the test and the step of a loop into one line, so a reader sees at a glance how a loop starts, when it ends and how it advances. Nearly every later language with braces copied it.
The switch statement has one notable trap. When a case matches, execution continues into the following cases unless the programmer writes break. The book points this out and treats falling through as something to use sparingly and to comment when used. Many bugs have come from a missing break, and several later languages changed the rule so that falling through must be explicit.
Taken together, the control flow shows the same attitude as the rest of the language. The constructs are few, they map directly onto jumps and tests in machine code, and they leave the programmer free, including free to make mistakes. A reader who understands exactly what each construct does at the machine level has understood this chapter.
Truth and zero
C has no separate type for true and false in the language the book describes. A test is false if its value is zero and true otherwise. Comparisons produce one or zero. This is simple and matches how machines test conditions, and it allows idioms such as looping while a pointer is not null or while a character is not the terminating zero. It also allows a classic error: writing a single equals sign, which assigns, where a double one, which compares, was meant. The assignment produces a value, the value is tested, and the program compiles without complaint. Modern compilers warn about this; the habit of reading conditions carefully is still worth building.
Idea three: pointers and arrays
Idea three: pointers and arrays
Pointers are the heart of C and the chapter on them is the heart of the book. A pointer is a variable that holds a memory address: the location of some other value. Through a pointer, a program can read or change the value it points to, without knowing where that value lives in advance.
An analogy helps. Think of memory as a very long street of numbered houses, each holding one small item. An ordinary variable is a name for one house. A pointer is a slip of paper with a house number written on it. You can copy the slip, pass it to a friend, add one to the number to get the next house, or go to the house and change what is inside. Many slips can carry the same number. A slip can also carry a number that belongs to no house at all, or to a house that has since been demolished, and following it then leads to trouble.
Why pointers matter
Pointers solve several problems at once. They let a function change a variable that belongs to its caller, since C passes arguments by copying their values: the caller passes the address, and the function changes what lives at that address. They let a program handle large data without copying it, by passing an address instead. They let a program build linked structures, where one item records where the next one is. And they let a program use memory allocated while it is running, whose location is only known at run time.
The link between arrays and pointers
The book's most distinctive teaching is the close relationship between arrays and pointers in C. An array is a block of consecutive values of the same type. In most expressions, the name of an array stands for the address of its first element. Indexing into the array is defined as adding the index to that address and looking at what is there. So the two ways of writing access to an element, with an index or with pointer arithmetic, mean the same thing.
Pointer arithmetic is scaled by type. Adding one to a pointer to an integer moves it forward by the size of one integer, not by one byte. This makes pointer arithmetic natural for walking through arrays: start at the first element and step forward until done.
This unity is elegant and efficient. It is also a frequent source of confusion, because arrays and pointers are not identical. An array is storage; a pointer is a variable holding an address. The book is careful about this distinction, and readers who rush tend to blur it. A good test of understanding is to explain why an array passed to a function arrives as a pointer, and what information is lost on the way, namely its length.
Strings as arrays
C has no string type. A string is an array of characters ending with a zero byte. All string functions in the library work by walking along the array until they find that zero. This is simple and efficient, but it has costs. Finding a string's length means scanning it. And if the zero is missing, or a string is copied into an array too small to hold it, the program writes past the end into whatever memory comes next. Many of the most serious security failures in computing history came from exactly this.
Pointers to functions and more complex declarations
C also allows pointers to functions, so a program can pass a function as an argument to another function. The book shows this with a sorting routine that can sort in different orders depending on which comparison function it is given. This is the same idea that later languages present as higher-order functions or callbacks, here expressed at the level of addresses.
With pointers to pointers, arrays of pointers and pointers to functions, C declarations can become hard to read. The book acknowledges this and shows how to read a declaration from the inside out, even giving a small program that translates declarations into words. The difficulty comes from a design choice: in C, a declaration is written to look like the way the variable is used. That principle is consistent, but it produces knots.
Idea four: functions and separate compilation
A C program is a collection of functions. One of them, called main, is where the program begins. Functions take arguments, which are copies of values, and may return a value. Large tasks are broken into small functions that each do one thing. The book shows this decomposition repeatedly, with programs built from pieces such as a function to read a line, a function to compare strings, and a function to print a result.
Scope and storage
The book explains carefully where variables live and who can see them. Variables declared inside a function are local: they exist while the function runs and vanish when it returns. Variables declared outside all functions are external: they exist for the whole run and can be shared among functions. The keyword static changes either case: a static local variable keeps its value between calls, and a static external variable or function is visible only in its own file.
These rules are the tools C offers for organising a large program. There are no modules or packages in the modern sense. Instead, a program is split into files, each file hides what it wants to keep private by marking it static, and shares what it wants to expose through declarations in header files that other files include.
Compiling in pieces
Each source file can be compiled separately into an object file, and a linker combines the object files and libraries into a program. This model is simple and has lasted. It makes it possible to build very large systems in which no single compilation needs to see the whole program, to reuse compiled libraries, and to rebuild only what changed. The Unix tool make, which rebuilds programs from their parts, grew out of exactly this way of working.
The preprocessor
Before compilation, a C source file passes through a preprocessor that handles file inclusion, macro substitution and conditional compilation. The book covers it briefly. It is a crude text-substitution tool, not part of the language proper, but in practice it is everywhere: header files are included with it, constants are often defined with it, and code is adapted to different machines with it. Its crudeness causes its own class of bugs, for example when a macro argument is evaluated twice. The book shows enough of these pitfalls to make a reader careful.
Function prototypes
The largest change between the two editions concerns functions. In the original language, a function's declaration did not list the types of its parameters, so the compiler could not check that a call passed the right kinds of arguments. Passing an integer where a floating-point number was expected compiled silently and produced nonsense. The ANSI standard added prototypes, borrowed from C++, which declare parameter types so the compiler can check calls and convert arguments. The second edition teaches the new style throughout. This change alone removed a large class of errors, and it is one strong reason to read the second edition.
Idea five: structures and data structures
A structure groups several values of possibly different types under one name. A point might have an x and a y. A record for a word in a table might have the word itself, a count, and a pointer to the next record. Structures let a program treat related data as one thing, pass it around, and keep arrays of it.
The real power appears when structures are combined with pointers. A structure can contain a pointer to another structure of the same type. That one feature makes it possible to build linked lists, where each item points to the next; binary trees, where each node points to two children; and hash tables, where an array of pointers leads to chains of items. The book builds several of these, including a binary tree that counts word occurrences and a table lookup of the kind used in a macro processor or a compiler's symbol table.
Building what the language leaves out
This is where C's smallness becomes a strength. The language has no built-in dictionaries or lists, but with structures, pointers and a memory allocator, the programmer can build exactly the data structure a problem needs, laid out in memory exactly as wanted, with no hidden overhead. Higher-level languages provide these structures ready-made, and the implementations inside those languages are very often written in C in just this way.
The book also covers unions, which let one piece of memory hold values of different types at different times, and bit-fields, which pack small values into a word. Both are tools for close control over memory layout, used in systems code and when matching the formats of hardware or files.
Memory allocation
Programs often do not know in advance how much memory they will need. C handles this through library functions that request a block of memory of a given size while the program runs and later release it. The book shows how such an allocator can be written, keeping a list of free blocks and carving requests out of them. Seeing it built demystifies what happens when any program, in any language, asks for memory.
Memory requested this way must be released by the programmer. Forgetting to release it causes leaks, where a long-running program grows until it fails. Releasing it twice, or using it after release, corrupts the allocator's bookkeeping and can cause crashes or security holes far from the original mistake. Later languages introduced garbage collection or ownership rules precisely to remove this burden.
Idea six: input, output and the library
C has no input or output statements. Reading and writing are done by calling functions in the standard library. The book introduces the simplest ones in the opening tour, reading and writing one character at a time, and builds surprisingly useful programs from them: copying input to output, counting lines, words and characters. A reader learns early that a great deal can be done with a small set of operations applied in a loop.
Streams
The library presents files as streams of characters. A program opens a file and gets back a handle, reads or writes through that handle, and closes it. Three streams are always open: standard input, standard output and standard error. This model came from Unix, where it allowed small programs to be chained together, each reading what the previous one wrote. It turned out to suit almost every operating system, and it is now the default model of input and output in most languages.
Formatted output and input
The library's formatted output function, which takes a format string with placeholders and a list of values, is one of the most copied designs in computing. Many later languages have functions that work the same way. Its matching input function, which parses text according to a format, is more troublesome, and experienced programmers use it carefully. The book explains both, along with functions for strings, character classes, mathematics and memory.
The library is just C
A recurring lesson of the book is that the library is not special. In the chapter on the operating system interface, the authors show how the stream functions can be written using the system's lower-level calls for reading and writing blocks of bytes. They also show a simple directory listing and the memory allocator mentioned earlier. The point is not that readers should replace the library. It is that they should understand that each layer of a system is made of ordinary code resting on the layer below. That understanding is the beginning of systems programming.
Idea seven: close to the machine, yet portable
C sits in an unusual place. It is close enough to the hardware that a good programmer can predict roughly what machine instructions each line will become. Yet it is not tied to any particular machine. A C program written carefully can be compiled and run on computers with different processors, different word sizes and different operating systems.
This combination was the reason for C's success with Unix. Once the Unix kernel was written in C, moving Unix to a new computer meant writing a C compiler for that computer and rewriting a small machine-dependent part of the kernel. The rest came along. By the late 1970s Unix had been moved to machines quite unlike the PDP-11, and C went with it. Universities used Unix for teaching, students learned C, and they took it into industry.
How it manages both
C achieves this by abstracting only what every machine has in common. Every machine has memory addressed in some unit, integers of some sizes, arithmetic, comparison, branching and subroutine calls. C gives names and structure to these things and nothing more. Where machines differ, the language either leaves the detail to the implementation or says the behaviour is undefined, meaning the program is wrong and anything may happen.
Undefined behaviour deserves a word, because it is often misunderstood. It does not mean that the result is random. It means the language makes no promise at all, so the compiler is free to assume the situation never arises. Modern optimising compilers take that freedom seriously, and a program that reads past the end of an array or overflows a signed integer may behave in ways that seem impossible from the source code. The book's era had simpler compilers, and its warnings about such cases read mildly today. A modern reader should treat them as strict.
A portable assembly language
C is sometimes called a portable assembly language. The phrase captures something true: C gives the control of assembly language while hiding the differences between machines. It is also misleading, because C has real types, real functions and a real grammar, and modern compilers transform C code heavily. A better way to put it is that C models an abstract machine that closely resembles real ones. Learning that abstract machine, with its bytes, addresses, stack and heap, is the most lasting thing a reader takes from this book, whatever language they go on to use.
Idea eight: the craft of clear technical writing
The book is admired not only for what it teaches but for how. It is often given as an example of technical writing at its best, and it is worth studying as writing even by someone who never writes C.
What the style consists of
Sentences are short and exact. Each one says one thing. There is almost no padding, no encouragement, no jokes beyond a dry aside here and there, and no repetition for emphasis. Terms are defined once and then used consistently. When a matter is subtle, the text slows down and explains it with an example, then moves on.
The examples are real. They are not fragments that illustrate a feature in isolation but complete programs that could be useful. A word counter, a line sorter, a calculator, a table lookup, a memory allocator. Each is short enough to read in a few minutes and good enough that one might keep it. The code is written in a consistent, compact style that became the model for a generation, including the placement of braces that many programmers still call K&R style.
The book also trusts the reader. It assumes intelligence and does not over-explain. This is part of why it is short, and also why some beginners find it hard. Reading it well requires attention: a sentence one skims may contain the key fact.
Why this matters on its own
Most technical documentation is either too long or too vague. This book shows that a precise subject can be explained completely in a short space, if the writer knows the subject deeply and chooses examples with care. Anyone who writes specifications, manuals, reports or teaching material can learn from it. The lesson is not to imitate its terseness mechanically but to imitate its decisions: what to include, what to leave out, and which example will make an idea stick.
Idea nine: small programs that fit together
Many of the book's example programs share a form. They read from standard input, transform what they read in some simple way, and write the result to standard output. A program counts lines. Another removes trailing blanks. Another converts tabs to spaces, or finds lines containing a pattern, or sorts lines. None of them is impressive alone. Together they show a way of building software that came from Unix and that the book teaches without ever making a speech about it.
Filters
A program of this kind is called a filter. Its input and output are plain text, so its output can become another filter's input. On Unix, the shell lets a user connect programs in a chain, so that a task like finding the ten most common words in a document can be done by joining a few small tools rather than writing a new program. Each tool does one job, does it well, and knows nothing about the others. The value lies in the connections.
The analogy is a kitchen with good knives, pans and bowls rather than one machine that makes only soup. With general tools and a habit of combining them, the cook can handle dishes no single machine was designed for. Specialised machines are faster for their one task, but the general kit adapts.
Why the book teaches this
The book's examples are small partly because a book has limited space. But the smallness also reflects a belief about software: that a program should be short enough to understand completely, and that larger capabilities should come from combining understood parts. The functions in the tutorial follow the same rule at a finer grain. A function to read a line, a function to copy a string, a function to find a pattern: each is a few lines long, has one purpose, and is used by several programs.
This idea is often confused with a simple preference for brevity. It is more specific than that. It is about interfaces. A text stream is an interface so simple that any program can use it. A function with a clear purpose and a small set of arguments is an interface that any caller can use. The design question is always where to draw the boundary so that the parts on either side can change independently.
Where it leads
This way of working influenced far more than Unix. The idea of composing small services with simple interfaces reappears in network protocols, in the design of libraries, and in the modern practice of building large systems from many small cooperating programs. It also has limits. Plain text is a weak format for structured data, and chains of filters can become hard to understand when they grow long. Later systems added richer formats and more structured ways of connecting programs. But the basic lesson, that clear boundaries between small parts make large systems manageable, has held.
Idea ten: the cost model in the programmer's head
A quieter lesson runs through the whole book. When you write a line of C, you can estimate what it costs. An assignment between two integers is one or two machine operations. A loop over an array touches each element once. A function call pushes arguments and a return address and jumps. A call to the memory allocator is more expensive, and a call into the operating system more expensive still. The language does very little that the programmer did not ask for.
This transparency is rare. In many higher-level languages a short line can hide a great deal of work: copying a large collection, allocating memory, looking up a method in a table, or pausing for garbage collection. That is often a fine trade, since it saves the programmer's time. But it means the programmer cannot easily predict performance by reading the code.
Why it matters beyond C
A programmer who has worked in C carries a rough cost model into every other language. They can ask what a high-level operation must be doing underneath, and guess when it will be slow. They understand why passing a large structure by value is expensive, why repeatedly growing a string can be quadratic, and why data laid out contiguously in memory is faster to process than data scattered through pointers. These intuitions come from having once worked at the level where every byte is visible.
The point is easy to overstate. Modern processors, with caches, pipelines and speculative execution, make exact prediction hard even in C, and modern compilers rewrite code aggressively. The cost model is a first approximation, not a precise account. But a first approximation is far better than none, and this book is one of the best ways to acquire it.
The tradition around it
The tradition around it
What came before
C grew out of a line of languages. ALGOL 60 introduced block structure and a clear grammar. CPL, a large language designed in Cambridge and London in the early 1960s, was too ambitious to implement fully. Martin Richards reduced it to BCPL, a small language for writing compilers, with a single data type, the machine word. Ken Thompson reduced BCPL further to B for the tiny PDP-7. Ritchie's C restored types to this lineage so it could fit the byte-addressed PDP-11. Each step made a language smaller or better matched to real machines.
Other systems languages existed at the time. Multics was written in PL/I, a very large language. Various manufacturers had their own system implementation languages. What set C apart was the combination of small size, enough expressive power to write an operating system, and its tie to Unix, which was spreading through universities.
What came after
C's influence is enormous. C++, begun by Bjarne Stroustrup at Bell Labs around 1979, extended C with classes and much more while keeping C as a near subset. Objective-C added a different object model on top of C. Java, C# and JavaScript took much of C's syntax: braces, the operators, the shape of loops and conditionals. Go, designed partly by Ken Thompson and Rob Pike, kept C's spirit of smallness while adding garbage collection and concurrency. Rust set out to offer C's control over memory without its memory errors.
The C language itself kept being revised through further standards, adding features such as fixed-size integer types, better support for different character sets, and a defined memory model for threads. Its core remains the language this book describes. A programmer who knows the second edition well can read nearly any C code written since.
Its neighbours on this shelf
Several books in this bay connect directly. The Undecidable, edited by Martin Davis, and Introduction to Automata Theory, Languages, and Computation by Hopcroft and Ullman describe computation in the abstract; this book shows the concrete language in which most real computation has been written. Compilers: Principles, Techniques, and Tools by Aho, Sethi and Ullman explains how a language like C is translated into machine instructions, and much of its practical material assumes C. Code by Charles Petzold builds up the machine from relays and gates, arriving at roughly the level where C begins. Computer Architecture: A Quantitative Approach by Hennessy and Patterson describes the processors that C programs run on, and reading the two together shows why C's choices made sense.
Structure and Interpretation of Computer Programs by Abelson and Sussman is a useful contrast. It teaches programming through Scheme, a language that hides the machine and emphasises abstraction. This book does the opposite, keeping the machine in view at all times. A reader who has absorbed both sees programming from two complementary angles. Structured Programming by Dahl, Dijkstra and Hoare argued for disciplined control flow and data; C adopted structured control flow while keeping the goto statement and leaving data discipline to the programmer. The Mythical Man-Month by Brooks describes the problems of building large systems; C's separate compilation and the Unix habit of small tools were partly an answer to those problems. Knuth's The Art of Computer Programming describes algorithms in a machine-level style, and C is a natural language in which to implement them.
Questions and criticisms
Safety
The strongest criticism of C is that it makes certain errors easy and their consequences severe. Buffer overflows, where data is written past the end of an array, have been behind many of the most damaging security attacks, from early network worms onward. Use of memory after it has been freed, uninitialised variables and integer overflows add to the list. These are not mistakes only beginners make. They appear in carefully written, heavily reviewed code. In recent years government security agencies and major software companies have publicly recommended moving new systems work to memory-safe languages where possible.
The book does not hide these dangers, but it was written for a smaller, more trusting world of computing, where programs ran on machines shared by colleagues rather than exposed to the internet. Some of its standard library examples use functions that are now considered unsafe, most notably a line-reading function with no way to limit how much it reads, which was later removed from the standard. A modern reader should learn the book's lessons and then learn the safer practices that grew up since.
Terseness
Some readers find the book too terse for a first encounter with programming. That is a fair point but not a defect, since the book never claims to be for beginners. Others criticise the compact style of C idioms that the book teaches, arguing that dense one-line loops are harder to read and maintain. There is a real tension here between the expressiveness a fluent reader enjoys and the clarity a newcomer needs. Most modern style guides lean toward clarity.
What has aged
The first edition's old-style function declarations are obsolete, which is why the second edition is the one to own. Some details of the Unix system interface chapter reflect Unix as it was in the 1980s. The book says nothing about threads, about the memory models needed for multiple processors, or about the size of modern programs and teams. It does not cover the later standards. None of this affects its core value, but a reader should pair it with current documentation for actual work.
What has held up
The model of memory, the treatment of pointers, the design of the library, the habits of decomposition into small functions, and the examples themselves have aged remarkably well. So has the writing. It is rare for a technical book to remain in print and in use for so long, and rarer still for it to remain the recommended text for its subject. That longevity is the best evidence of its quality.
Why it is on this shelf
The bay this book sits in is about the foundations of computing. Most books in it are about ideas: what can be computed, how information is measured, how machines might think. This one is about how computing is actually done, at the layer just above the hardware. We keep it because ideas about computation become real only when someone writes them down in a language a machine can run, and for fifty years that language has very often been C.
For a reader whose interests run across Vedanta, Jyotish, science, economics and technology, the book offers a particular discipline. Vedanta teaches attention to what lies beneath appearances. Jyotish rests on exact calculation of positions and intervals, and the software that computes them is usually written in C or something close to it. Economics and science today depend on numerical programs whose inner loops are written in C or languages built on it. Understanding that layer means one can look under any program and know, roughly, what is there.
We also keep it as a model of writing. It shows that difficult technical matter can be stated briefly and exactly, without either hiding complexity or inflating it. That is a standard worth keeping in view for any serious work.
There is one more reason. Much of this shelf asks how a system of rules gives rise to behaviour: how a few axioms yield a body of mathematics, how a few principles of economics explain a market, how a few positions and periods yield a horoscope. C is a clear modern case of the same pattern. A few types, operators and statements, combined according to fixed rules, produce operating systems and databases. Studying how so much comes from so little is useful training for thinking about any rule-governed system.
Finally, the book is honest about its limits. It does not claim C is suited to every task, and it does not oversell. That calm tone, a tool described by the people who made it with neither apology nor boast, fits the spirit in which we want the whole shelf to be read.
How to read it
Before you start
Get the second edition, the one marked as covering ANSI C. Install a C compiler; every common operating system has one freely available. Have a text editor and a terminal ready. This book is not meant to be read away from a computer.
A first pass
- Read the opening tutorial chapter over a week or two. Type in every example by hand, compile it and run it. Do not copy and paste; typing forces you to see every character.
- Do at least half the exercises in that chapter. They are short. If one takes more than an hour, move on and return later.
- Read the chapters on types and on control flow quickly. Much will feel familiar from the tutorial. Note the conversion rules and the operator precedence table, which you will consult often.
- Slow down for functions and program structure. Split one of your earlier programs into several files and compile them separately.
- Give the chapter on pointers and arrays the most time of all, perhaps two or three weeks. Draw memory diagrams with a pencil: boxes for variables, arrows for pointers. Work through each example by tracing what happens to each box.
- Read the chapter on structures alongside a small project of your own, such as a word-frequency counter using a binary tree.
- Skim the input and output chapter and the Unix interface chapter on first reading. Return to them when you need them.
A second pass
After working through the main chapters, read the reference manual straight through once. It will feel dry, but it will settle many questions you did not know you had. Then use it as a reference while writing your own programs. On this second pass, read the code examples again and ask why each line is written the way it is. Many small decisions in them repay attention.
What to read alongside and next
Pair the book with a modern guide to safe C practice, since the book predates current security concerns. From this shelf, read Code by Charles Petzold first if the machine itself feels abstract. Then read Computer Architecture: A Quantitative Approach for the processors underneath, and Compilers: Principles, Techniques, and Tools to see how C becomes machine code. For a contrasting philosophy of programming, read Structure and Interpretation of Computer Programs. For the problems of building large systems, read The Mythical Man-Month.
Questions to carry
- Which features did C leave out, and what did leaving each one out gain and cost?
- Why does an array lose its length when passed to a function, and what follows from that for safe programming?
- How is a pointer different from an array, even though they can often be used the same way?
- What does it mean for behaviour to be undefined, and why might a compiler exploit it?
- Why did writing Unix in C make Unix portable, and why was that considered risky in 1973?
- Which parts of the standard library could you write yourself, and which depend on the operating system?
- How does C's model of memory, with stack, heap and static storage, show up in languages that hide it?
- When is a compact idiom clearer than an explicit one, and when is it not?
- What problems did function prototypes solve, and what does that say about type checking in general?
- Why has C lasted so long when many better-designed languages have not?
- What would a language need to offer to replace C in operating system kernels, and what would it have to give up?
- How does the book's choice of examples teach habits as well as features?
- Where does responsibility for correctness lie in C, and is that the right place?
Words to know
- Pointer: a variable holding the memory address of another value.
- Array: a block of consecutive values of one type, accessed by index.
- Null-terminated string: an array of characters ending with a zero byte, which is how C represents text.
- Pointer arithmetic: adding to or subtracting from a pointer, scaled by the size of the type it points to.
- Structure: a group of values of possibly different types gathered under one name.
- Union: a type whose members share the same memory, so only one is meaningful at a time.
- Header file: a file of declarations included by several source files so they agree on shared names.
- Preprocessor: the text-processing stage before compilation that handles includes, macros and conditional code.
- Macro: a name the preprocessor replaces with text before the compiler sees the program.
- Function prototype: a declaration giving a function's parameter types so the compiler can check calls.
- Linker: the program that combines separately compiled object files and libraries into one executable.
- Static: a keyword that keeps a local variable alive between calls, or limits a name to its own file.
- Stack: the memory region where local variables live during a function call.
- Heap: the memory region from which a program requests space at run time and must later release it.
- Undefined behaviour: program actions for which the language promises nothing, so any result is possible.
- Buffer overflow: writing past the end of an array into adjacent memory.
- Memory leak: allocated memory that is never released, so a program's use of memory grows.
- Standard library: the set of functions every conforming C implementation must provide.
- ANSI C: the language as standardised in 1989, which the second edition describes.
- K&R C: the earlier dialect defined by the first edition of this book.
- Stream: a sequence of characters read from or written to a file or device.
- Portability: the ability of a program to be compiled and run on different machines with little change.
- Unix: the operating system for which C was created and with which it spread.
A note on this edition
This is a study edition written for this shelf in our own words. It describes and discusses The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie but does not reproduce it. The book itself is in copyright. It is still in print, widely available, and worth owning; a library or bookshop will have it. Read the original with a compiler open beside it. Nothing here replaces typing in its programs and watching them run.