Variable length arg lists for macros
Larry Jones
scjones at sdrc.UUCP
Fri Sep 16 08:40:17 AEST 1988
In article <362 at island.uu.net>, walker at island.uu.net (Richard Walker) writes:
> 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)
Well, it's a good idea, but the example could be better - it could be valid
C if the replacement text didn't have the parentheses around a partial
statement! In any case, the preferred definition of statement macros is:
#define COMPLEX_MACRO(foo) do { whatever } while (0)
That way the macro expands into a single statement, eats the following semi-
colon, and causes a syntax error if there isn't one instead of accidentally
swallowing the following statement.
----
Larry Jones UUCP: uunet!sdrc!scjones
SDRC scjones at sdrc.uucp
2000 Eastman Dr. BIX: ltl
Milford, OH 45150 AT&T: (513) 576-2070
"Save the Quayles" - Mark Russell
More information about the Comp.std.c
mailing list