Using Macros
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Wed Aug 8 16:18:11 AEST 1990
In article <14339 at shlump.nac.dec.com>, farrell at onedge.enet.dec.com (Bernard Farrell) writes:
> In article <362.26be9dcc at astro.pc.ab.com>, yoke at astro.pc.ab.com (Michael Yoke) writes...
[asks about defining macros that expand to more than one statement]
> };
>
> The presence of an empty statement after
> the closing brace should not cause any problems,
I wasn't going to reply to this, but the Official Free Answer[*] is
to use the expansion
do { /* your statements go here */ } while (0)
This will work in any context where a statement is allowed; it wants
a semi-colon after it just like a simple function call would. For
example:
#define swap(Type,x,y) do { \
Type *ZZQ1 = &(x); \
Type *ZZQ2 = &(y); \
Type ZZQ3 = *ZZQ1; \
*ZZQ1 = *ZZQ2; *ZZQ2 = ZZQ3; \
} while (0)
(Exercise for the reader: why did I write 3 declarations instead of 1?)
[*] An Official Free Answer is something you find in an FSF manual,
such as the manual for CCCP.
--
Taphonomy begins at death.
More information about the Comp.lang.c
mailing list