preprocessor directives in macros

John Garnett garnett at a.cs.okstate.edu
Mon Dec 5 09:19:55 AEST 1988


>From article <122 at rdahp.UUCP>, by geoff at rdahp.UUCP (Geoff Walsh):
> 
> I tried to use the preprocessor with something like:
> 
> #define diagprintf(TestCriteria,TestLevel,Fmt) \
>     #if (TestCriteria >= TestLevel)\
>         printf s\
>     #endif
> 
> to get the preprocessor to eliminate dead code AND the strings associated
> with Fmt, but the compiler didn't like this.  I get an error message
> like "Expected formal macro parameter" with one compiler, and " # operator

How about using something like the following:

#ifdef DEBUG
#define diagprintf(TestCriteria,TestLevel,Fmt) \
	if (TestCriteria >= TestLevel) {printf Fmt; fflush(stdout); }
#else
#define diagprintf(TestCriteria,TestLevel,Fmt)
#endif

Then you can control whether or not the debug code is even compiled
by using 'cc -DDEBUG code.c' to include debug code or 'cc code.c'
to prevent the code from being sent to the compiler.

John Garnett


~ John Garnett                         Internet: garnett at a.cs.okstate.edu
~ Computing and Information Sciences   UUCP: {cbosgd, ihnp4, rutgers}!
~ Oklahoma State University                   okstate!garnett



More information about the Comp.lang.c mailing list