Using Macros
Stephen Clamage
steve at taumet.com
Fri Aug 10 02:34:43 AEST 1990
gg10 at prism.gatech.EDU (GALLOWAY) writes:
>Why doesn't this work?
>#define A_MACRO(bar, baz) func1(bar), func2(baz)
>I have been using syntax like this for awhile, is this wrong or unportable?
You need an extra set of parens:
#define A_MACRO(bar, baz) ( func1(bar), func2(baz) )
Look what happens without the parens:
i = A_MACRO(3, 4) + 2;
becomes:
i = func1(3), func2(4) + 2;
which is the same as
(i = func1(3)), (func2(4) + 2);
which is probably not what was meant. With parens, it becomes;
i = (func1(3), func2(4)) + 2;
which may or may not be what was meant, but at least the +2 is not discarded.
If the do { ... } while(0) construct was used instead, the above
expression becomes illegal, which may or may not be what was intended.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list