Date: Sat, 26 Feb 2000 13:03:41 +0000 From: Ben Smithurst <ben@scientia.demon.co.uk> To: Alfred Perlstein <bright@wintelcom.net> Cc: cjclark@home.com, Marco Molteni <molter@csl.sri.com>, freebsd-chat@FreeBSD.ORG Subject: Re: how to do this C preprocessor trick? Message-ID: <20000226130341.B98536@strontium.scientia.demon.co.uk> In-Reply-To: <20000225234909.W21720@fw.wintelcom.net> References: <20000225182432.A5017@sofia.csl.sri.com> <20000226001121.A20702@cc942873-a.ewndsr1.nj.home.com> <20000225214616.U21720@fw.wintelcom.net> <20000226003741.C20702@cc942873-a.ewndsr1.nj.home.com> <20000225234909.W21720@fw.wintelcom.net>
next in thread | previous in thread | raw e-mail | index | archive | help
Alfred Perlstein wrote:
> * Crist J. Clark <cjc@cc942873-a.ewndsr1.nj.home.com> [000225 22:02] wrote:
>>
>> Why a,
>>
>> do { <stuff> } while(0)
>>
>> Rather than just,
>>
>> { <stuff> }
>
> I really don't remeber offhand, I know a lot of macros in FreeBSD
> are moved from
>
> #define foo { bar; baz; }
>
> to
>
> #define foo do{ bar; baz; }while(0)
with the original #define,
if (some_condition)
foo;
else
bar();
would expand to,
if (some_condition)
{ bar; baz; };
else
bar();
That is a syntax error (extra semicolon before "else"). I used to use
the form without do/while until this bit me. :-) With the second form,
well you can work it out, and the expanded code is perfectly legal.
--
Ben Smithurst / ben@scientia.demon.co.uk / PGP: 0x99392F7D
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-chat" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000226130341.B98536>
