K and R - The C Answer Book
The C Programming Language
by Kernighan and Ritchie
Reviewed September 2023
- Stands the test of time
- Respects the reader
- while still being approachable
- Has examples, examples, and more examples
Disclaimer
Please remember this book was published in 1988!
It’s absolutely fabulous as an intro. Some of the advanced topics are out of date, however, and some of the practices are outdated.
An example is putting all variable declarations at the top of a file - this is now an anti-pattern as it ineffectively uses memory. It had a place in the past, but no longer. Another example is declaring variables in if/else-if statements - this is also not recommended, as it causes unexpected side-effects and can be hard to read. A better method is to define variables as-needed around if/else-if statements and use the variables. Exceptions, of course, are made for loop-contained variables, especially indices and similar in for/while loops. In general though, don’t have your code have side-effects!
The K&R was written by the people who designed and wrote C. These people are legends! And their insight into their language is amazing. So do read the book! But remember - it’s not the end-all.
That said, here’s a short reading guide:
Chapters 1, 2, 3, and 4
are your basics of C. Syntax, types, loops, functions, and basic compilation - you’ll do a hello-world here, for example.
Pitfall topics:
-
Variables and scope are easy mess up in C, and the resulting errors can be a hassle to identify.
-
Keep in mind your use of static – and how it differs if it’s inside a block vs applied to a declaration or global!
-
Dig around in the compilation process – take a look at the results of preprocessing (gcc -E), at the object dump from your assembling (gcc -c, then objdump -d file.o), and at the object dump from your linked executable.
Chapter 5
is pointers! Grab your towel and Don’t Panic. This is a well-paced walkthrough, give it a slow and purposeful read.
Pitfall topics:
-
are just what you’d expect – pointers are easy to mess up and hard to debug.
-
Remember – &variable gives you the address, *variable gives you the value at a pointer, and * in a declaration indicates a pointer.
-
C is pass-by-value! So everything is pointers - or pointers-to-pointers, even.
-
Array notation and pointer notation are mostly interchangeable – and as long as the compiler is made aware of the type the pointer references, adding to a pointer will move sizeof() !
Chapter 6
is structures - in the age of object-oriented-everything, structs should be pretty conceptually comfy.
Pitfall topics:
- Self-referential structs require careful malloc/free – the compiler can’t be expected to know how large an object is when it contains itself, after all!
Chapter 7 and 8
cover I/O and UNIX system interfacing. These chapters are very useful but often outdated – not usually incorrect, but there are many modern developments that ease the more tricky topics in these chapters. That said, do read them, as they cover the extreme-basics. The examples in chapter 8 are especially informative. Set aside time to walk through these - maybe even try running them yourself, they’re that decent.
Remaining contents
are appendices including libraries and references manuals. I find these chapters are a good skim - but I have trouble remembering these hyper-detailed tidbits out of context/use. I did enjoy skimming, though - some of the referenced materials are weird! Especially some of the more esotheric operators.
Additionally, the Standard Library appendix does have some useful tables for printf/scanf formatting options. These are always nice to have handy to flip open.