In the ISO C standard of 1999, a macro can be declared to accept a variable number of arguments. This allows you for example to define the following debugging output macro:
1 2 3 4 5 6 7 8 9 |
/* Print debugging output, if DEBUG is defined */ #define DEBUG /* Debugging output macro */ #ifdef DEBUG #define debug(...) { (void) fprintf (stdout, __VA_ARGS__); } #else #define debug(...) ((void) 0) #endif |
To use the macro in your code, simply use it like printf:
debug("The Answer to the Ultimate Question is '%d'\n", answer); |
It is of course possible to achieve the same behaviour with a function, where you could wrap the call to fprintf between ifdef DEBUG and endif. But this has the overhead of calling a function, whereas the preprocessor replaces every occurrence of debug(...) by ((void) 0) if DEBUG is undefined.
Instead of defining DEBUG inside your code, you could also the -DDEBUG preprocessor option of GCC.