Programming Books, part 4: The C Programming Language

It’s 32 years old, and it remains the single greatest book ever written about a programming language.  Its crown is secure; even if you’d not already read the title of this article, you’d know what book I’m talking about.  It’s the only language-specific book in Top Five programming books of the Programming Reddit’s FAQ.  Co-written by Reinvigorated Programmer regular Brian W. Kernighan and Dennis M. Ritchie, it’s not just the definitive book about the language in question, it’s the book the rewrote the book on what it means to be definitive.  Step forward, please, The C Programming Language! [amazon.com, amazon.co.uk]

The biography of the Beatles at allmusic.com has a very astute and resonant bit of analysis right in the first paragraph, saying that “they were among the few artists of any discipline that were simultaneously the best at what they did and the most popular at what they did.”  You could say the same for K&R, as it’s affectionately known: everyone knows it’s the best book on C, and (for once) the thing that everyone knows is actually true.

So what makes it so great?

Short, comprehensive, dense

First: it’s so short.  At 272 pages (this is for the second edition, published in 1988 and describing ANSI C), it’s shorter that Harry Potter and the Prisoner of Azkaban (317 pages) and little more than one third the length of Order of the Phoenix.

Second, it’s so comprehensive.  There is, essentially, nothing to be known about C beyond what is in this book.  If you can read those 272 pages, and understand them all, then you are well on the way to being a C wizard.  (Er, assuming you have the patience to go on to accumulate a decade of experience leading to wisdom, taste, good judgement and technical intuition.)

Third, and this is really a consequence of the first two, it’s so dense.  This is not a book that wastes words.  There are no extended introductory sections on Why You Should Learn C and C’s Place In The World.  The two prefaces (for 1st and 2nd editions) are one and a bit pages each.  The introduction is four pages.  Then we’re straight into Chapter 1. A Tutorial Introduction, which is 30 pages long and in that space covers:

  • 1.1 Getting Started
  • 1.2 Variables and Arithmetic Expressions
  • 1.3 The For Statement
  • 1.4 Symbolic Constants
  • 1.5 Character Input and Output
  • 1.6 Arrays
  • 1.7 Functions
  • 1.8 Arguments — Call by Value
  • 1.9 Character Arrays
  • 1.10 External Variables and Scope

At the end of that chapter, on page 34, is a sequence of five exercises, culminating in this one (and enjoy the characteristic Kernighanian understatement in the final sentence):

Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces.  Don’t forget about quotes, both single and double, escape sequences, and comments.  (This program is hard if you do it in full generality.)

And, as tough as that may seem after only 30 pages, they really have given you all the tools you need to do the exercise by this point.

Say what you mean, simply and directly

Apologies if you’re getting bored of reading this Kernighan-and-Plauger epigram every time you return to this blog, but I really don’t think it can be over-emphasised.  Although this advice’s appearance in The Elements of Programming Style is of course in the context of writing programs, Kernighan also follows his own advice when it comes to writing prose.  No words are wasted; neither is your time.  Yet somehow the book avoids feeling rushed despite packing so much into so little space.

After the tutorial introduction, the remaining chapters cover:

  • Chapter 2.  Types, Operators, and Expressions
  • Chapter 3.  Control Flow
  • Chapter 4.  Functions and Program Structure
  • Chapter 5.  Pointers and Arrays [this, by the way, on page 93]
  • Chapter 6.  Structures
  • Chapter 7.  Input and Output
  • Chapter 8.  The UNIX System Interface

That’s it for the chapters.  So they’ve got you doing systems programming by page 169; from page 185 to the end of the chapter, they show you how to implement malloc().  These guys are not messing about.

And then it’s on to the appendices, which rival those of The Return of the King for comprehensiveness (though thankfully without the notes on the differences between Eldar and Númenorean calendars.)

  • Appendix A.  Reference Manual [because all the chapters are tutorial]
  • Appendix B.  Standard Library [yes, all of it, in 18 pages]
  • Appendix C.  Summary of Changes [since the 1st edition]

And finally, there’s just time for a characteristically comprehensive index before the book comes to a close.

In praise of small

Kernighan and Ritchie’s much-quoted preface explains the philosophy behind the book’s characteristically dense structure:

We have tried to retain the brevity of the first edition.  C is not a big language, and it is not well served by a big book.  […]  Appendix A, the reference manual, is not the standard, but our attempt to convey the essentials of the standard in a smaller space.  […]  As we said in the preface to the first edition, C “wears well as one’s experience with it grows.”  With a decade more experience, we still feel that way.

And it’s true that the book is only able to be as short as it is because the language that it describes is as small as it is.  I have the second edition of Stroustrup’s The C++ Programming Language, which clearly models itself on K&R and is about as terse as such a book can be, but its 691 pages make it fully two and half times the size of the original.  This, mind you, is the second edition of Stroustrup, published in 1991 only three years after the K&R second edition, when C++ was still a relatively well under control.  (I have not dared look beyond the cover of the current, 3rd, edition, but I see from Amazon that it weighs in at 1040 pages, and I am not in the least surprised.)

There is much, much more that I could say about the smallness of C, but rather that go against everything I’ve just been saying by bloating this review up into a monster, I am going to save that for a separate article.

Do it yourself

It’s also characteristic of K&R that they have this statement on the copyright page:

This book was typeset (pic|tbl|eqn|troff -ms) in Times Roman and Courier by the authors, using an Autologic APS-5 phototypesetter and a DEC VAX 8550 running the 9th Edition of the UNIX(R) operating system.

That they did their own typesetting is not just a cute touch, but an insight on the completeness of their mastery of what they were doing, and the care they took over it.  The book is not what you would call beautiful to look at, but the typesetting is wholly functional, at one with the text rather than fighting against it.  (Also, this statement is a gentle indirect boast, as Kernighan wrote the pic program and co-wrote eqn with Lorinda Cherry.)

(Kernighan, left, railing against innumeracy; Ritchie, right, auditioning for the role of Saruman.)

If I could analyse it, I’d do it myself

Finally, we come to the aspect of The C Programming Language that is hardest to explain — and hardest to do.

The bottom line here is that writing is an art.  You can hack your way through to producing tolerable text without being an artist, just as an uninspired programmer can bash his way through to wiring together an uninspired web application.   But just as it takes a Ken Thompson to invent and write UNIX, and a Dennis Ritchie to invent C and write the initial compiler, so it takes a Brian Kernighan to write The C Programming Language.

(Kernighan, by the way, is consistently very clear that he had no part in the birth of C, a common misapprehension due to his co-authorship of the book; but reading between the lines, it seems that he was very much the main writer of K&R.)

If all it took to write a truly great technical book was to write down everything there is to say about a subject and then ruthlessly distill it to its essence, then great technical books would be much less rare than they are.  That, I think, is a prerequsite; but it’s Necessary But Not Sufficient.  There is a graceful quality about the writing in K&R, even when it is brutally technical; it draws you on and in; it’s just pleasant to read.  It is, on occasion, gently humorous, though certainly not written for laughs the way that, say, Programming Perl is.  It’s exhilarating how the book takes you somewhere worth getting to, and does it so quickly.  It treats you like a grown-up; it is not “For Dummies”, but its intelligent approach is not the elitist kind that seems to want to make the reader feel inferior, but a warm intelligence that lifts you up to its level.  In short, it’s a book that wants to make you a better programmer.

The best way I can express it is to say that at the end of each section and subsection, you want to read on and find out what’s next.  That stands in stark contrast to too many other technical books, where I find myself peeking ahead to find out how much more of the current chapter there is to plough through before I can stop reading.

I wish I knew how they did it.  But I’m glad that they did.  Kernighan and Ritchie, we salute you!

Update (later the same day)

Nice dicussion on Hacker News, and some comments on Reddit.  As always there are dissenting opinions, but it’s been interesting to see the strong consensus that K&R really is the classic that I see it as.

(I add these updates with links to other discussions partly for my own benefit, to make it easier to track down what’s being said elsewhere.  Are they useful to others, too, or should I just keep the links in private notes rather than appending them to articles?)

About these ads

47 responses to “Programming Books, part 4: The C Programming Language

  1. The Ruby Programming Language was also written by two persons, one of whom created the language. It is a bit longer (400 pages) for a much larger language. It’s also great.

  2. Stroustrup’s tome, at its 3rd edition, is definitely not for the faint of heart. I took it up on myself, as a fairly experienced C programmer, to read it from cover to cover in three weeks for my C++ college course; and I don’t regret saying I faltered, and skipped the parts on the STL and software design in order to bring my suffering to a halt.

    On the other hand K&R is, if not a timeless masterpiece, one that will be very close to the heart of every C programmer who likes his prose, as his languages, served simple and to the point. Thank you, Brian and Dennis!

  3. There is, essentially, nothing to be known about C beyond what is in this book.

    Wrong. It’s up to date with regard to 1980s idioms, but a lot have been invented since then and are (obviously) not discussed in there at all. A large amount of my time consists of producing various forms of ADT and other object-style thing using structure hiding, sometimes even going so far as to implement polymorphism. You’ll look for that in vain, IIRC (and the former technique at least is *very* common, verging on ubiquitous). A good few of their allegedly-common C idioms, especially in the domain of string handling, are unnecessarily terse, and some I have never seen outside the pages of K&R (thank goodness).

    My carping notwithstanding, it is a very very very good book, as good as it could have been given its publication date; it surely deserves its place in the canon.

  4. Agree with your assessment about this book. It’s probably the most concise reference. Contrast that with Stroustrup’s tomes, that seem to be saying a lot without giving any new insights.

  5. Interesting!
    I read K&R after Wirth’s “Pascal: User manual and report”, and K&R struck me as being slightly verbose:-) I seem to have mislaid my copy, but I recall it was just under 200 pages (around 1977, probably 2nd edition).

    What also struck me was that the structure of the books was identical. But: I didn’t realise this until a lot later, when I had read many other books so I became aware that this wasn’t exactly the standard way to go about writing a language definition:-)

  6. “It’s the only language-specific book in Top Five programming books of the Programming Reddit’s FAQ. ”

    SICP is hardly language inespecific, isn’t it?

  7. PauloC, I have to admit that I’ve not read SICP myself, so I could be mistaken. But the impression I’ve got from everything I’ve read about it is that, while its example code is all in Scheme, it’s not about Scheme, but about the theory of computation, with Scheme merely the tool used to make that theory concrete. Am I wrong?

  8. Thanks Mike for this post!
    I have read R&K lots of years ago and it has been enlighten!
    My impressions agree with yours. I perfecly remind being so excited while reading the book.(the same way an adventure romance does…)

  9. The final (fifth) chapter of SICP is about programming non-lisp/scheme machines. Ironically it starts with writing a Scheme emulator for assembly language.

    The book isn’t about Scheme per se, but it certainly is Scheme-centric without being Scheme-specific.

  10. At my first professional job, a wise old Vax Checz hacker gave me his copy and said, “This is the best written book in the English language, fuck Shakespeare”

  11. Heresy, perhaps, but I think K&R is in some areas *too* terse. Harbison & Steele’s “C: A Reference Manual” is IMO much better in explaining the C standard library, providing an example of usage and a full discussion of each function rather than a 1-sentence summary.

  12. Open it up and write a quote in the first page.
    Something from the news, something you will look at years from now. (I have a Regan quote in my K&R, “The US must either automate, immigrate or liquidate”)

  13. While I agree that K&R is a good book, there are still innumerable gotchas (like shifts by values greater than or equal to the variable size) and portability headaches in the language that could be partially mitigated by a thorough read of the C9X spec.

  14. Pingback: 32 years on, K&R’s “The C Programming Language” still stands alone

  15. Menger Subscribes

    Minor correction: 3rd paragraph from the bottom, can you please correct the word “Neccessary”? it should be “Necessary”, i.e. a single “c”.

    Otherwise, I enjoy reading the whole article.

    [Mike says: thanks for spotting that; now fixed.]

  16. http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

    Apparently C is the #1 programming language on the TIOBE index currently

  17. The K&R book is a classic. Not quite timeless but several orders of magnitude better than anything else.

    Having read the K&R book, I bought myself a copy of Schildt’s Advanced C book. It was an utter waste of money. A colleague of mine said that he preferred the Schildt book to the K&R one. He found it hard to find things in the K&R one but the Schildt book had examples everywhere.

    That is one of the things about the K&R book; it is concise. Nothing is repeated. All material about a topic is handled in a few pages and then it moves on. There is no going back and fore over the same old ground over and over again.

    I think that this is the main reason I like the book so much. It says what it means quickly and then moves on. It requires a motivated reader to get the best out of it, so if you like your books overflowing with useless examples, that have no teaching value whatsoever, it is not the book for you.

    I always liked the fact they did their own typesetting. I would like to do my work docs in groff but in these days of MS-Word, where the ability of others to [excuse me while I rant] screw up your document is considered a right in team environments, it isn’t a goer.

  18. Disapointed with SICP

    SICP is a good book, but does not cover Scheme. Specifically it says nothing about how to write Scheme macros.

    Considering I was initially interested in Lisp dialects and SICP to learn how to use macros this was somewhat of a disappointment.

  19. Another favourite book of mine is ‘Programming in Lua’. Another small language, and another book that is not that large (328 pages) but manages to get all the intricacies and possibilities of the language across with clarity and humour. IHMO, it ranks pretty close to K&R.

  20. “gently humerous”? What are you, some kind of bone obssessive?

  21. Thanks, Sacha, good spot — now fixed.

    (For the rest of you: Sacha knows perfectly well that I am indeed a bone obsessive — see for example http://svpow.wordpress.com/2009/07/01/things-to-make-and-do-part-1-pig-skull-off-topic/

  22. Not sure using Reddit’s list as an argument works since they include the worst book on programming available: Code Complete. That thing is enough to bore otherwise promising developers completely out of the industry.

    Still, K&R is king. That much is hard to argue :)

  23. While I agree that The C Programming Language is a very good example of how to write a book on a computer language, I am far from certain that I would call it a programming book: It only deals with programming as far is necessary to explain C. Similarly, I would not call (the hypothetical book) The English Language a book on writing. (This not intended to pick nits, but to point to a danger of unintentionally misleading readers of the above post.)

    @Dude
    I actually consider Code Complete (in a late 90s edition) to be quite valuable, and would recommend it as one of the first books to read for the beginning professional.

  24. @michaeleriksson

    I agree about R&K not being a generic programmin’ book but I have to point the fact that the way the language is explained, the examples, the algorithms (.. the perfect consistency.. oh.. ah.. I have to read it again soon) has left to me a lot of notions about “the programming” art in general.

  25. I wish there were more books like K&R.

    Stroustrup 3rd ed. is indeed terrible. It mixes up teaching C++ syntax with trying to cover OO principles and the author’s rambling thoughts on good programming practice. It never fails to send me to sleep (which is unfortunate if I opened it looking for some specific information).

    Another interesting and readable C book: van der Linden’s ‘Deep C Secrets’.

  26. The Lua book is definitely good. The Schildt book is definitely awful, like pretty much everything he has ever written on the subject. Yes, it does have examples everywhere. It’s a shame they’re mostly wrong, if not actively dangerous.

  27. Both the first and the second editions have an honored place on my bookshelf. I still refer to the second edition from time to time. I also like E&S’s “The Annotated C++ Reference Manual”, for most of the same reasons I like “The C Programming Language”. I have both the original and ANSI Base Document editions of this as well. Along with these, my most visited shelf holds copies of Codd & Date (a half-dozen volumes on relational database theory and languages) and of course Knuth’s “The Art of Computer Programming”. The last volumes of note on that shelf is Wirth’s “Algorithms+Data Structures=Programs” and Kernighan and Plauger’s “Software Tools”. Gee, it almost sounds like a software engineering graduate course book list! :-)

  28. Pingback: Top Posts — WordPress.com

  29. Pingback: Kernighan & Ritchie

  30. Pingback: Frameworks and leaky abstractions « The Reinvigorated Programmer

  31. Pingback: The long-overdue serious attempt at Lisp, part 2: is Lisp just too hard? « The Reinvigorated Programmer

  32. I first learned C from a 30 page paper by Dennis Ritchie before the first edition of K&R came out. K&R doesn’t seem terse when approached from that angle.

  33. migla9, that is fantastic! Do you know if that paper is still available anywhere? Is it this one? http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf

  34. Yes, that looks like it. Thanks for the link! I think my original printed copy got left behind when I left my previous employer.

  35. Pingback: Are you one of the 10% of programmers who can write a binary search? « The Reinvigorated Programmer

  36. Pingback: Para aprender C « Mbpfernand0's Blog

  37. i agree with you fully, while at the university I read another similar book which is same as this, that is Signal ans Systems. Both in K&R and Signal ans Systems facts were to the point. No BS.

  38. Pingback: The long-overdue serious attempt at The Silmarillion, part 1: what it isn’t and what it is « The Reinvigorated Programmer

  39. I don’t like “the C programming language”.

    The reason is that C is a truly horrible language, and while this book could (should) give some warning/guidence on its pitfalls, it actually does the opposite & hides them from the novice.

    An excellent example is…

    void (*signal(int sig, void (*func)(int)) ) (int);

    This monstrosity is from the standard signal library, and was the first standard library routine that I came across while designing my first (and only) big C program. Not one of the C programmers I have known have been able to explain to me what this means, and K&R give little or no help on these problems.

    By far & away the best book on C is “Deep C Secrets” by Peter Van Der Linden. No serious C programmer should be without it. Just to confirm how good it is, it devotes an *entire chapter* to analysing the above signal function definition, which goes on to give a foolproof method of “unscrambling C declarations”. Its only when you see the syntactic mess explained in this excellent book, that the true horror of C becomes apparent. I think most C programmers stunble their way through the language in ignorance of its truly horrific dark underbelly.

  40. Mark, I am at a bit of a loss to understand how you find K&R’s treatment of complex declarations deficient. In fact they (A) explain how they work, (B) apologise for the syntax, which they consider a misfeature of the language, and (C) provide a pair of programs, right there in the text, for translating between C declarations and English phrases like “declare x as pointer to function returning int”. What more do you ask?

  41. they “apologise for the syntax”(!) and they “provide a pair of programs, right there in the text, for translating between C declarations and English phrases”(!)… I rest my case.

    I really do recommend you get hold of “Deep C Secrets” if you still ever have to program in C (or even if you don’t) – its a great book. If only all programming language books were this good, the world would be a better place… :-)

    regards,
    Mark.

  42. they “apologise for the syntax”(!) and they “provide a pair of programs, right there in the text, for translating between C declarations and English phrases”(!)… I rest my case.

    I don’t understand what your case is. K&R is a book about C programming languagew (clue’s in the title) which has awkward syntax for complex declarations. To K&R explain what that syntax is without trying to argue that it was well designed, and provide tools to make it easier to work with — plus, in classic, efficient K&R style, the sections about those tools also serve to further educate the reader about the syntax that they translate.

    Explain again how that is in any way deficient? It seems perfect to me.

  43. It may be that my view of the book is prejudiced by my view of the language (which I admit are & should be different things). I really do believe that C & its horde of mutant offspring have held back the progress of software engineering for at least 20 years. In my view it has all the appearance of having been designed over one weekend, as quick hack, to get some other job more easily than it would otherwise have been using assembly language (that job being writing Unix).

    In a nutshell, I suppose my view derives from the disparity between the book and the language. To the C novice (as I was when I first read it), it appears to be a well written book that gives a clear description of an elegant programming language. It was only when I finally got round to learning C properly by writing a large industrial strength program, that I realised what I was up against. And its that “deceit” that I object to. I believe that the majority of the bugs in the main two OS’s in use today (Windows & Unix), can be directly attributed to use of the C language and its poorly designed standard libraries.

    By contrast, Modula-2 *really was* a well designed language that did exactly the same job as C. I used it through the 1980’s for my weekend hobby programming (at work I used COBOL!), and I had a really elegant & short book, that described the language fully in about 100 pages. I no longer have the book and I’ve been unable to find the title. Modula-2 was disliked by many people, because of its “pickiness”. I agree it was not perfect (which language is?), but once you began to realise the fact that 90% of the time its pickiness was alerting you to guenuine bugs that you would otherwise have had to find the hard way, you began to appreciate its benefits. My big C program is a Unix text editor, that I started in about 1994. I am still finding bugs today in code that was written 10 years ago. By contrast my Modula-2 (and Pascal) programs had far fewer bugs by the time they had compiled cleanly – I would estimate maybe even *an order of magnitude* fewer.

    I think we will just have to agree to differ.

    Regards,
    Mark.

  44. As noted, K&R is NOT for the faint hearted! However, it provides, almost like a true precis, the entire C language in a way that is unambiguous, methodical, and thorough. I got my first copy in 1982 when I started C programming, and still refer to it today (the 2nd edition). Both 1st and 2nd editions have places of honor on my bookshelf, right next to E&S’s ARM for C++.

  45. Pingback: Programming Books, part 5: Programming Pearls | The Reinvigorated Programmer

  46. christi parks

    I am not a programmer but I have this C language subject this session and have to prepare for it. What all topics should be covered in it?
    And has anyone studied from this course http://www.wiziq.com/course/2118-learn-how-to-program-in-c-language of C tutorial online?? or tell me any other guidance…
    would really appreciate help

  47. Pingback: So what actually is my favourite programming language? | The Reinvigorated Programmer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s