🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5332

Re: Variable length arg lists for macros

#5332
From: Larry Jones
Date: Thu, 15 Sep 1988 22:40
28 lines
1711 bytes
In article <362@island.uu.net>, walker@island.uu.net (Richard Walker) writes:
> The problem illustrated above is caused by the bad usage of the macro. The
> macro call should be formed as a statement to avoid the flow control problem.
> In other words, the errors above are caused by the missing ';' at the end
> of the trace macro call.
> 
> Especially, macros which expand into code with flow control
> should be formed such that the semicolon cleanly terminates
> the flow, e.g.:
> 
> #define COMPLEX_MACRO(foo) (if(SOME_TEST(foo)) {statements;}else)

Well, it's a good idea, but the example could be better - it could be valid
C if the replacement text didn't have the parentheses around a partial
statement!  In any case, the preferred definition of statement macros is:

#define COMPLEX_MACRO(foo) do { whatever } while (0)

That way the macro expands into a single statement, eats the following semi-
colon, and causes a syntax error if there isn't one instead of accidentally
swallowing the following statement.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@sdrc.uucp
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
"Save the Quayles" - Mark Russell
Technical Details
Message-ID: <386@sdrc.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!sdrc!scjones
References: <438@ucsvc.unimelb.edu.au> <1036@cbnews.ATT.COM> <228@itivax.UUCP> <362@island.uu.net>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!sdrc!scjones
From: scjones@sdrc.UUCP (Larry Jones)
Newsgroups: comp.std.c,comp.lang.c
Subject: Re: Variable length arg lists for macros
Message-ID: <386@sdrc.UUCP>
Date: 15 Sep 88 22:40:17 GMT
References: <438@ucsvc.unimelb.edu.au> <1036@cbnews.ATT.COM> <228@itivax.UUCP> <362@island.uu.net>
Organization: Structural Dynamics Research Corp., Cincinnati
Lines: 28