Nested Macros

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Wed Nov 22 02:01:54 AEST 1989


In article <2913 at taux01.UUCP> yval%taux01 at nsc.nsc.com (Yuval Yarom) writes:

| He should use:
| 
| #define DEBUG 1
| 
| #ifdef DEBUG
| #define DB(X) printf(X)
| #else
| #define DB(X) /* */
| #endif

  That doesn't seem to work if there are multiple arguments. The macro
is called with too many args. Then you rewrite the macro to delete
parens and use double parens in the call:

	#ifdef DEBUG
	#define DB(X) printf X
	#else
	#define DB(X) /* */
	#endif

	DB(("slimit: %d\n", slimit));

and the whole thing gets ugly. Or you can define it as:

#ifdef	DEBUG
#define	DB	printf
#else
#define DB	if (0) printf
#endif

  Or something like that. This removes the possible problem of having
to put the args in double parens (you WILL forget once in a while), and
most compilers will not even compile code for the test or the
never-executed printf call. I realize that that's not universally true,
so the original suggestion, modified to handle multiple arguments,
would be appropriate.

  You can extend this in the obvious way to something like:

	#define DB(condition, list) if (condition) printf list

  I usually like to use a more readable name for a macro which does so
much, like "bugtrace" or something.
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list