Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Mar 1998 17:49:42 +0100
From:      lennartz.electronic@t-online.de (Thomas Zenker)
To:        freebsd-current@FreeBSD.ORG
Subject:   Re: A question about sys/sys/queue.h
Message-ID:  <19980313174942.53822@tue.le>
In-Reply-To: <19980313121330.54903@follo.net>; from Eivind Eklund on Fri, Mar 13, 1998 at 12:13:30PM %2B0100
References:  <XFMail.980312191745.shimon@simon-shapiro.org> <19980313121330.54903@follo.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Mar 13, 1998 at 12:13:30PM +0100, Eivind Eklund wrote:
> On Thu, Mar 12, 1998 at 07:17:45PM -0800, Simon Shapiro wrote:
> > Why was the definition of some macros changed
> > from:
> > 
> > #define FOO(a) {   ...   }
> > 
> > to:
> > 
> > #define FOO(a) do {  ...  } while(0)
> > 
> > I thought these are the same...
> 
> Imagine these used in a dual if () statement:
> 
> if (bar)
> 	if (baz)
> 		FOO(1);
> 	else
> 		printf ("You loose!\n");
> 
> With the former, you get something that (with proper indentation) map
> as
> 
> if (bar)
> 	if (baz)
> 		{ ... };
> else
> 	printf ("You loose!\n");
> 
> while with the do {...} while (0) trick, you get
> 
> if (bar)
> 	if (baz)
> 		do { ... } while(0);
> 	else
> 		printf ("You loose!\n");
> 
It will not compile anyway.
The version w/ "{ ... };" is syntactically incorrect, because
there are two statements after the if, leaving the else w/o partner.

if (bar)
        if (baz)
                { ... }   /* compound_statement           */
;                         /* empty but existing statment! */
else                      /* else without matching if!    */
...

while the second version "do { ... } while (0);" still is a simple statement.
-- 
Thomas Zenker
at work         thz@lennartz-electronic.de
private         thz@tuebingen.netsurf.de

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19980313174942.53822>