Article View: comp.std.c
Article #5330Re: Variable length arg lists for macros
From: Richard Walker
Date: Wed, 14 Sep 1988 21:49
Date: Wed, 14 Sep 1988 21:49
33 lines
1650 bytes
1650 bytes
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 <flames_about_crufty_usage_of_code_in_macros >/dev/null
Technical Details
Message-ID:
<362@island.uu.net>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!island!walkerReferences:
<438@ucsvc.unimelb.edu.au> <1036@cbnews.ATT.COM> <228@itivax.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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