On Learning C, Part 4: What Should I Read? Why Should I believe you?

Table of Contents

Why should I believe you?

When it comes to C, I am quite proficient. I’ve been employed professionally to write C. I created a somewhat popular open source project in C. I have extensive experience profiling C programs12, optimizing them34, and making them multithreaded5. I’ve contributed to other open source C projects. In short, if I’m not worth taking seriously on this topic, then very few people are.

How I Learned C

C was my first real programming language.6 I was introduced to it at the age of 13. Thanks to some luck and an advanced placement test, I was allowed to take up to two classes per semester at Gonzaga University. Wanting to learn more about programming, I enrolled in CS121.

My student ID

I stuck out a little on campus.

I distinctly remember an early assignment where I was completely stumped by a bug. I’d almost finished the program, but there was one issue that I couldn’t fix. An if statement was always evaluating to TRUE, even when it shouldn’t. The else was never taken. The program compiled without warnings. It was incredibly frustrating.

I spent two days staring at that code. I didn’t know about debuggers, so I peppered my code with printf()s. I commented and uncommented chunks of code. No matter what I tried, I simply couldn’t understand why the program was misbehaving. I was almost in tears when I asked my dad for help. He saw the problem in seconds:

if (a = b) {
} else {

I had a single equals in a conditional. That meant I was assigning a to b instead of comparing them. As soon as I added another equals, my program worked flawlessly. All that effort and frustration was caused by a single missing character.7

I’m still surprised that, afterwards, I remained interested in writing code. I’ve quoted him before, but Douglas Crockford said it best:

I think there has to be something seriously wrong with you in order to do this work. A normal person, once they’ve looked into the abyss, will say, “I’m done. This is stupid. I’m going to do something else.” But not us, ‘cause there’s something really wrong with us.

This was undoubtedly the most difficult educational experience of my life. I really did learn C the hard way.

So what should I read?

Honestly? I’m not sure. For those who want to to learn C, I have yet to find a book that I can unconditionally recommend. The only text I can personally suggest is the Kernighan and Ritchie book from part 1. As I said in that post, caveats apply.

For those who know some C and are looking to improve their skills, there are better resources. I’ve heard good things about Robert Love’s Linux Kernel Development (PDF). And though it covers much more than C, Computer Systems: A Programmer’s Perspective will teach you all the gory details of how source code gets turned into machine code.

The dearth of good C books is a bit of a bummer, but there may be a silver lining. I think many learners rely too much on books. It’s often more educational to poke around on your own. Read other people’s code. Examine open source projects. Ask others for help. And if you’re not sure how something works, do science!

  1. Making Ag Faster: Profiling with Valgrind 

  2. Profiling with Gprof 

  3. Optimizing Ag: Special-casing File Extensions 

  4. Profiling Ag. Writing My Own Scandir 

  5. The Silver Searcher: Adding Pthreads 

  6. Before that, I’d only written a few toy programs in Logo, QBasic, and TI-BASIC

  7. If you’re wondering why the compiler didn’t warn about this, it’s because this happened in 1998. Back then, gcc didn’t warn about assignments in conditionals. Nowadays, any sane compiler will complain. How fortunate one is to learn C today. 🙂 

When commenting, remember: Is it true? Is it necessary? Is it kind?