What I’m Reading: Serious about C

So I’ve decided it’s time to learn C, for real. I used to know only enough to be dangerous. At the little startups I worked at, it was enough to know that a pointer was a reference to a memory address to impress people, but I couldn’t read real source code, and I didn’t know the kind of things Real Programmers know, like algorithms, linked lists, binary trees, etc. Discussions of those topics, Donald Knuth’s books notwithstanding, are in C, and my Java knowledge, such as it was, did not enable me to get past the first line that looked like this:

getfile(void (*fill)(char *, long), (*skip)(char *, long))

So I’ve been cozying up with The C Primer Plus, Fifth Edition, by Stephen Prata, and Code Reading, the Open Source Perspective by Diomidis Spinellis. The former is excellent, and the later… the jury’s still out.

I’ve known for a long time that C is primarily concerned with modeling the way the modern CPU works. It isn’t a language for high-level conceptual abstractions, or if it is, those abstractions are awfully leaky considering how you have to worry about memory allocation and freeing, pointers, array lengths, and so on. C was intended to save hackers from programming in assembly languages, and it succeeded. But you still have to know what you’re doing.

The C Primer has taken me from 0 to about 40mph with C in a month. I love the way each chapter focuses on a set of functions or concepts and remains focused on them. Even when a more advanced topic is briefly introduced, the author keeps it simple so I don’t get completely lost at the start. I’ve hardly noticed the effort of progressing from sprintf() to advanced data types. Even with my less-than-perfect attention span, I can recall each step of “how I got here” as new, complicated subjects are introduced. And it helps that Prata has a good sense of humor, too.

I want to read, and possibly even hack, the source code to the major open source software applications I use regularly. I want to know more about how Apache and Mozilla, for instance, work internally. I was really impressed when Chad Fowler answered a question about Ruby by popping open the C source to find out. That’s why I’ve decided it’s time to learn C. Code Reading is advertised as a primer on reading source code. How do you parse someone else’s code when there are no comments and the style is not your own?

I have mixed feelings about the book. On the one hand, a chapter about pointers and all the ways they can be used, backed up with source code from Unix apps, is very helpful. (Most of the code Spinellis offers is in C, so it’s a good compliment to The C Primer.) I was especially pleased with the discussion of binary search and the use of variants and invariants to analyze looping control structures–meat and potatoes!

But there isn’t as much emphasis on strategies for approaching strange code as I’d hoped. And the upcoming chapters on coding conventions and documentation don’t promise to be all that different from the stuff you read in other programming books. We’ll see.