Path: utzoo!utgpu!water!watmath!clyde!burl!codas!uflorida!gatech!bbn!diamond.bbn.com!mlandau From: mlandau@bbn.com (Matt Landau) Newsgroups: comp.lang.c Subject: Re: C Quirk?? Message-ID: <10643@jade.BBN.COM> Date: 17 Feb 88 20:14:01 GMT References: <1653@ssc-vax.UUCP> <804@PT.CS.CMU.EDU> <23498@hi.unm.edu> <186@ateng.UUCP> <885@PT.CS.CMU.EDU> <413@fig.bbn.com> Reply-To: mlandau@bbn.com (Matt Landau) Organization: BBN Laboratories Incorporated, Cambridge, MA Lines: 22 In comp.lang.c (<413@fig.bbn.com>), rsalz@bbn.com (Rich Salz) writes: > >As a general rule, you probably don't want to use { } in #define's >as { }; is not the same as { }. One trick that works really nicely is: #define COMPLEX_MACRO() do {statement-group} while (0) Since "do {} while (0)" is a single statement, you can nest this inside if's without worrying about getting the nesting wrong, missing or extra semicolons (you just always follow the invokation with one, just as though it were a function call), etc. It's guaranteed that the statement-group will be executed exactly once, and any compiler worth its salt will figure out that it can eliminate the test and generate the equivalent of inline code. Of course, lint complains bitterly, but you can't have everything. -- Matt Landau Waiting for a flash of enlightenment mlandau@bbn.com in all this blood and thunder