Path: utzoo!attcan!uunet!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: Nested Macros Message-ID: <1680@crdos1.crd.ge.COM> Date: 21 Nov 89 15:01:54 GMT References: <2913@taux01.UUCP> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: GE Corp R&D Center, Schenectady NY Lines: 50 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