Multi-statement macros (again)
Michael T. Sullivan
sullivan at aqdata.uucp
Fri Feb 9 10:50:51 AEST 1990
One of the packages we use has some macros for returning values
from a function. You call these macros when you want to return
instead of calling return. An example is:
#define new_return(n) s1; s2; return (something)
This just wouldn't do so I put braces around the statements. Recent
discussion here led me to change this macro to:
#define BEGIN_BLOCK do {
#define END_BLOCK } while (0)
#define new_return(n) BEGIN_BLOCK s1; s2; return (something); END_BLOCK
Problem with this particular macro is that because it has a return in it,
we would get a "statement not reached" warning from the compiler. I then
changed BEGIN_ and END_BLOCK to:
#define BEGIN_BLOCK do { if (1) {
#define END_BLOCK } } while (0)
This seems to make everything happy. My question is, will this make other
machines happy? We are using a 3B2/400 SVR3 and it didn't even complain
when just braces were used (resulting in "};"). Does this shut everything
up on YOUR machine?
P.S. That BEGIN_BLOCK/END_BLOCK stuff really DOES come in handy.
--
Michael Sullivan uunet!jarthur!aqdata!sullivan
aQdata, Inc. sullivan at aqdata.uucp
San Dimas, CA +1 714 599 9992
More information about the Comp.lang.c
mailing list