A common trick is to do this:
#ifdef DEBUG
#define OUTPUT(x) printf x
#else
#define OUTPUT(x)
#endif
#include <stdio.h>
int main(void)
{
OUTPUT(("%s line %i
", __FILE__, __LINE__));
return 0;
}
This way you have the whole power of printf()
available to you, but you have to put up with the double brackets to make the macro work.
The point of the double brackets is this: you need one set to indicate that it s a macro call, but you can t have an indeterminate number of arguments in a macro in C89. However, by putting the arguments in their own set of brackets they get interpreted as a single argument. When the macro is expanded when DEBUG
is defined, the replacement text is the word printf
followed by the singl argument, which is actually several items in brackets. The brackets then get interpreted as the brackets needed in the printf
function call, so it all works out.