Calling multiple functions in a macro.
T. William Wells
bill at twwells.uucp
Wed Nov 2 11:12:52 AEST 1988
In article <353 at marob.MASA.COM> daveh at marob.masa.com (Dave Hammond) writes:
: In attempting to construct a macro which calls several functions,
: but can be used as a single statement (i.e. without braces in an if/else
: construct), I can see 2 possible alternatives:
:
: 1.
: #define FOO() do { foo1(); foo2(); foo3() foo4(); } while(0)
:
: 2.
: #define FOO() foo1(), foo2(), foo3(), foo4()
:
: The first adds quite a bit of code to the program and makes lint
: complain about constants in conditional text.
The first is unlikely to add that much to the code. Go look at the
assembly output.
: The second compiles, executes and lints correctly (on Xenix), but
: is it portable to string function calls in this manner?
Yes it is portable. But save yourself some headaches and write it:
#define FOO() (foo1(), foo2(), foo3(), foo4())
The extra parentheses can prevent problems with, e.g.:
a + FOO()
which would otherwise add a to the result of foo1 and toss the
result, when what you want it to do is add it to foo4 and return the
result.
---
Bill
{uunet|novavax}!proxftl!twwells!bill
More information about the Comp.lang.c
mailing list