On printf debugging

“Printf debugging” refers to the practice of adding output statements to your code, usually to show the value of a certain variable or that you’ve reached a certain point in the code.

For example:

void myFunction(int arg1, int arg2) {
  printf("Reached myFunction!\n");
  // rest of the function

Although the “printf” command is specific to C, I’ve found that people generally use the phrase “printf debugging” regardless of the language they’re working in.

This technique gets a lot of hate in some circles from people who say it’s better to learn to use a real debugger. Here’s my take on that:


This might surprise you, given some of my hardline stances on other issues in recent posts. Should you eventually learn to use a real debugger? Probably. But literally every person I know who writes code for a living uses printf debugging. Is it perfect? No. Does it get the job done for a lot of common cases, especially for beginners? You better believe it does.

Sticking to the C world, let’s talk about gdb for a bit. If you’re not familiar, it’s a command-line debugger for C and other languages. It’s also about as easy to use as your typical space shuttle control panel. When I taught beginning programmers, gdb was one of the lessons with the highest numbers of weeping and teeth-gnashing every semester. The idea of the lesson was to get students thinking about watching variable values, which is a good idea. But are there other ways we could get that information?

Don’t get me wrong – I’m not completely opposed to the use of debuggers. But I don’t think beginning developers need to worry about them too much. Generally, by the time you’re doing the kind of programming that might need a real debugger, you’re also using an IDE (integrated development environment, e.g., Eclipse or NetBeans) with a debugger that is much easier to use than something like gdb.

By the way, I don’t think IDEs should be introduced too early for beginning programmers, either (back to reclaiming my hardline credibility). This might be because I come from the last generation to remember when a command-line interface was all there was, but I think there’s something to be said for typing things out and getting that muscle memory in your fingers.

Zed Shaw, the person behind Learn Code The Hard Way, agrees. In discouraging readers from copying and pasting the provided code samples, Shaw explains, “The point of these exercises is to train your hands, your brain, and your mind in how to read, write, and see code.” Those are the most important skills to develop when you’re starting out coding, and copying and pasting won’t help you build those skills.

So to bring it full-circle, I’m in favor of printf debugging, even though it’s kind of a blunt instrument. One of the biggest hurdles that beginning programmers have to conquer is learning how to think like a computer, and printf debugging provides a window, however crude, into what’s actually going on inside a program. I can’t be mad at that.

Do you have any debugging experiences (printf or otherwise) that you’d like to share? Fire away in the comments!