Using Macros
Tim McDaniel
mcdaniel at adi.com
Fri Aug 10 08:08:27 AEST 1990
martin at mwtech.UUCP (Martin Weitzel) writes:
> As I allready recommended, such macros should not be used very often.
> In fact, I can see only two places where they sometimes may be
> desirable:
c) You need to do something which is impossible without use of the
preprocessor. The canonical example is an "assert(x)" that does
not evaluate its argument at all if assertion checking is turned
off.
d) You want to make some kind of syntax change to the language. This
should be done with EXTREME care! (The canonical counterexample is
the original, damn near unreadable, Bourne shell source code.)
Our example is "MESSAGE(fooBar);" which expands to
char fooBar = "fooBar";
In that way, we can
1) do
formatMessage(fooBar, ...);
to get the same effect as
formatMessage("fooBar", ...);
2) get a compile-time error rather than a run-time error if we
make a typo in a message name.
3) in another section do a check like
if (message == fooBar) ... /* pointer equality */
rather than
if (!strcmp(message, "fooBar")) ... /* string equality */
--
"I'm not a nerd -- I'm 'socially challenged'."
Tim McDaniel
Internet: mcdaniel at adi.com UUCP: {uunet,sharkey}!amara!mcdaniel
More information about the Comp.lang.c
mailing list