(from the printf() function in C) Used to describe debugging work done by inserting commands that output more or less carefully chose status information at key points in the program flow, observing that information and deducing what's wrong, based on that information.

Usually considered amateurish and inferior to the use of a real debugger, but has the advantage that it is always a workable option, e.g. when there is no (or no free, or no working) debugger available. Also, it offers greater flexibility than the limited interface of a debugger. The downside is that it usually takes longer (sometimes much longer) to pinpoint the problem.

This method is useful for tracking the flow of control and amounts to the same as using TRACE macros in Visual C++.
Put a printf(...) at the beginning and end of each function, possibly at the beginning of loops and if and switch blocks.
The more adventurous out there might try using fprint(...) so that the messages don't interfere with the output on the screen, or so the information can persist.

On a formatting issue, I usually insert these statements at the very beginning of the line, ignoring indentation so that the debugging feature can be easily spotted as such in the source code because it is so out of place.

Why use printf() to debug, asks -brazil-, if you've got a real debugger?

  • Some programs are unrunnable sans optimization. They just take too long. And debugging information either doesn't exist with optimization, or is plain confusing (optimization mixes different source code lines).
  • Some bugs don't show up when you compile with different options. Pointer bugs especially fall into this category.
  • You can leave debugging printfs (suitably #ifdef'ed out) in place for the next time. An interactive session goes away when you type `quit'.
  • Sometimes it takes several hours to get to the buggy place. printf() debugging is batch debugging.

I'll give up printf() when you pry my cold dead fingers from it!

I agree completely with what ariels said, but I think a bit more emphasis on the third point is appropriate.

If you're coding something with a life expectancy longer than a few weeks: Never ever do printf() debugging without your favorite precompiler tags around the printf() statement! Use #ifdef DEBUG or whatever, but make sure that the default compilation will skip the printf() lines.

This suggestion comes from personal experience with systems suddenly crashing after the debug messages had flooded the disk. This usually happened when a programmer delivered a patch and forgot to remove his debug code. With the precompiler tags, this would never have happened.

Log in or register to write something here or to contact authors.