[comp.lang.c] Nested Macros

jb3o+@andrew.cmu.edu (Jon Allen Boone) (11/20/89)

A friend is attempting to write a macro along the following lines:

#define DEBUG 1

#define DB(X) #ifdef (DEBUG) printf(X)

however, this results in an error when he attempts to compile it.

does anyone know how he would go about writing a macro that would
allow him to simply type DB(x) in his code and not have to type #ifdef
(DEBUG) printf(X) everywhere instead? Any help would be greatly
appreciated.  Please carbon copy your reply to pg0p@andrew.cmu.edu, as
well as to jb3o@andrew.cmu.edu and the board.

thank you...

yval@tasu23.UUCP (Yuval Yarom) (11/21/89)

In article <gZO1Sce00W0JQ6AKEr@andrew.cmu.edu> jb3o+@andrew.cmu.edu (Jon Allen Boone) writes:
>A friend is attempting to write a macro along the following lines:
>
>#define DEBUG 1
>
>#define DB(X) #ifdef (DEBUG) printf(X)
>
>however, this results in an error when he attempts to compile it.
>
>does anyone know how he would go about writing a macro that would
>allow him to simply type DB(x) in his code and not have to type #ifdef
>(DEBUG) printf(X) everywhere instead? Any help would be greatly
>appreciated.  Please carbon copy your reply to pg0p@andrew.cmu.edu, as
>well as to jb3o@andrew.cmu.edu and the board.
>
>thank you...

He should use:

#define DEBUG 1

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

--
Yuval Yarom
yval%taux01@nsc.nsc.com

wittig@gmdzi.UUCP (Georg Wittig) (11/21/89)

jb3o+@andrew.cmu.edu (Jon Allen Boone) writes:
>#define DEBUG 1
>#define DB(X) #ifdef (DEBUG) printf(X)
>does anyone know how he would go about writing a macro that would
>allow him to simply type DB(x) in his code and not have to type #ifdef
>(DEBUG) printf(X) everywhere instead?

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

-- 
Georg Wittig   GMD-Z1.BI   P.O. Box 1240   D-5205 St. Augustin 1 (West Germany)
email: wittig@gmdzi.uucp   phone: (+49 2241) 14-2294
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Freedom's just another word for nothing left to lose" (Kris Kristofferson)

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (11/21/89)

In article <2913@taux01.UUCP> yval%taux01@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@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

darcy@bbm.UUCP (D'Arcy Cain) (11/23/89)

In article <gZO1Sce00W0JQ6AKEr@andrew.cmu.edu> 
jb3o+@andrew.cmu.edu (Jon Allen Boone) writes:
>#define DEBUG 1
>
>#define DB(X) #ifdef (DEBUG) printf(X)
>

I would have coded the above  as follows just because I don't like
smart-alecky code (or more accurately I'm afraid some compiler won't :-).)

#ifdef DEBUG
#define DB(X) printf(X)
#else
#define DB(X)

This however still has the problem that printf has a variable number of
arguments so you  have to call it like this:

    DB(("Error: Result code is %d\n", res));   /* note double parentheses */

and that is probably your error.

D'Arcy