Xref: utzoo comp.std.c:374 comp.lang.c:12593 Path: utzoo!attcan!uunet!island!walker From: walker@island.uu.net (Richard Walker) Newsgroups: comp.std.c,comp.lang.c Subject: Re: Variable length arg lists for macros Message-ID: <362@island.uu.net> Date: 14 Sep 88 21:49:33 GMT References: <438@ucsvc.unimelb.edu.au> <1036@cbnews.ATT.COM> <228@itivax.UUCP> Reply-To: walker@island.uu.net (Richard Walker) Organization: Island Graphics, Marin County, California Lines: 33 In article <228@itivax.UUCP> scs@itivax.UUCP (Steve C. Simmons) writes: >In article <1036@cbnews.ATT.COM> lvc@cbnews.ATT.COM (Lawrence V. Cipriani) writes: >-In article <438@ucsvc.unimelb.edu.au>, u5565522@ucsvc.unimelb.edu.au (David Clunie) writes: >- >- #ifdef TRACE >- #define trace(anything) anything >- #else >- #define trace(anything) >- #endif >- > if ( x == m ) > trace( printf( "the consequences\n) ; ) > froob( some function ) ; > >This produced rather different programs depending on the definition of >'trace'! The problem illustrated above is caused by the bad usage of the macro. The macro call should be formed as a statement to avoid the flow control problem. In other words, the errors above are caused by the missing ';' at the end of the trace macro call. Especially, macros which expand into code with flow control should be formed such that the semicolon cleanly terminates the flow, e.g.: #define COMPLEX_MACRO(foo) (if(SOME_TEST(foo)) {statements;}else) Then the correct usage of COMPLEX_MACRO would be: COMPLEX_MACRO(foo); which also handles the case where COMPLEX_MACRO expands into nothing. cat /dev/null