Calling multiple functions in a macro.

Knudsen knudsen at ihlpl.ATT.COM
Wed Nov 2 05:43:25 AEST 1988


In article <353 at marob.MASA.COM>, daveh at marob.MASA.COM (Dave Hammond) writes:


> #define FOO()	do { foo1(); foo2(); foo3() foo4(); } while(0)

Why use the do and while?  C has blocks, you can say just plain
	{foo1(); ... foo4();}
Of course all you're really doing is supplying the braces so the
macro can be used "without braces" in the source.
I dimly recall defining macros wrapped in {}; they worked fine.


> #define FOO()	foo1(), foo2(), foo3(), foo4()
               (                              )
This should work as-is, and also yield the value of foo4().
I'd wrap the definition in parentheses, though, as shown.

I'd say that in the first example, {} are the sequential
equivalent to () in the second, in the sense of keeping you
out of trouble in any context.
-- 
Mike Knudsen  Bell Labs(AT&T)   att!ihlpl!knudsen
"Lawyers are like nuclear bombs and PClones.  Nobody likes them,
but the other guy's got one, so I better get one too."



More information about the Comp.lang.c mailing list