Using Macros
Matthew Landau
mlandau at bbn.com
Wed Aug 8 06:00:19 AEST 1990
Seems like this should be on the FAQ list, since it comes up every
couple of months. My preferred solution to the problem (which I
picked up in comp.lang.c about 10 years ago :-) is:
#define FOO(bar, baz) do { func1(bar); func2(baz); } while (0)
Since there's no trailing semicolon after the while(0), this construct
can be used anywhere a simple C statement can be used. (Indeed,
syntactically it is one statement.) Moreover, any compiler worth its
salt should be able to optimize away the do ... while part, since the
code above is guaranteed to execute the stuff inside the braces exactly
once.
Of course lint and Saber will complain bitterly, but you can shut
Saber up with judicious use of the SUPPRESS directive in the macro
definition. Still haven't figured out how to get lint to quiet
down, but then again I don't use lint much now that I have Saber.
--
Matt Landau Oblivion gallops closer,
mlandau at bbn.com favoring the spur, sparing the rein.
More information about the Comp.lang.c
mailing list