Using Macros

James C Burley burley at world.std.com
Wed Aug 8 10:34:11 AEST 1990


In article <14404 at diamond.BBN.COM> mlandau at bbn.com (Matthew Landau) writes:

   Seems like this should be on the FAQ list, since it comes up every 
   couple of months.  My preferred solution to the problem (which I 
   picked up in comp.lang.c about 10 years ago :-) is:

      #define FOO(bar, baz)  do { func1(bar); func2(baz); } while (0)

Hmmm, when I first saw the problem posted, I thought I knew the easy and
consistent answer, but all these other responses differ from mine.

Can anyone explain to me why

    #define FOO(bar,baz) (func1(bar), func2(baz))

wouldn't work in all the situations one could reasonably expect?  The macro
still expands to a single expression (as did the old version mentioned in the
original posting), right?  So a trailing semicolon causes it to be
interpreted as a statement, right?  The comma within the expansion serves
the same purpose of defining a sequence point as would the semicolon in other
solutions, right?  I have the horrible feeling I'm missing something, but I do
think I've done this before and it worked okay.

James Craig Burley, Software Craftsperson    burley at world.std.com



More information about the Comp.lang.c mailing list