Variable length arg lists for macros

Richard Walker walker at island.uu.net
Thu Sep 15 07:49:33 AEST 1988


In article <228 at itivax.UUCP> scs at itivax.UUCP (Steve C. Simmons) writes:
>In article <1036 at cbnews.ATT.COM> lvc at cbnews.ATT.COM (Lawrence V. Cipriani) writes:
>-In article <438 at ucsvc.unimelb.edu.au>, u5565522 at 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 <flames_about_crufty_usage_of_code_in_macros >/dev/null



More information about the Comp.std.c mailing list