Debug Print Macro in C?

I’ve seen this idiom a fair amount:

#ifdef DEBUG
# define DEBUG_PRINT(x) printf x
#else
# define DEBUG_PRINT(x) do {} while (0)
#endif

Use it like:

DEBUG_PRINT(("var1: %d; var2: %d; str: %s\n", var1, var2, str));

The extra parentheses are necessary, because some older C compilers don’t support var-args in macros.

Leave a Comment