Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Print Macro in C?

Tags:

c

macros

in C, what is the proper way to define a printf like macro that will print only when DEBUG symbol is defined?

#ifdef DEBUG #define DEBUG_PRINT(???) ??? #else #define DEBUG_PRINT(???) ??? #endif 

where ??? is where I am not sure what to fill in

like image 243
John Avatar asked Dec 21 '09 16:12

John


People also ask

What is printf debugging?

Yes - it's known as printf() debugging, named after the ubiquitous C function: Used to describe debugging work done by inserting commands that output more or less carefully chosen status information at key points in the program flow, observing that information and deducing what's wrong based on that information.

How do I debug a macro in Visual Studio?

Open up a source file from your manage project in Visual Studio and set a breakpoint on a line. Start debugging in Visual Studio by pressing F5. In Excel, open up your worksheet and start debugging your VBA code using Excel's debugger.


1 Answers

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.

like image 148
Aidan Cully Avatar answered Oct 02 '22 16:10

Aidan Cully