Alex Rivera | Logout

#define macro for debug printing in C?

Asked 2009-10-29T16:17:32.077
265

Trying to create a macro which can be used for print debug messages when DEBUG is defined, like the following pseudo code:

#define DEBUG 1
#define debug_print(args ...) if (DEBUG) fprintf(stderr, args)

How is this accomplished with a macro?

Edit
Report

1 Answer

9

According to http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html, there should be a ## before __VA_ARGS__.

Otherwise, a macro #define dbg_print(format, ...) printf(format, __VA_ARGS__) will not compile the following example: dbg_print("hello world");.

answered 2012-02-09T09:20:06.020

Your Answer