Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Multi-instructions macro and compiler warning (http://www.velocityreviews.com/forums/t754949-re-multi-instructions-macro-and-compiler-warning.html)

Jean-Christophe 10-16-2011 05:02 PM

Re: Multi-instructions macro and compiler warning
 
On 16 oct, 09:09, pozz

> I received many warning messages of the type
> * *W1207C: conditional expression is constant: dowhile
> Those messages refer to multi-instructions macros defined as below:
> * *#define MACRO(a, b) *do { foo(a); bar(b); } while(0)


Why not

#define MACRO(a,b) { foo(a); bar(b); }

Kleuskes & Moos 10-16-2011 05:44 PM

Re: Multi-instructions macro and compiler warning
 
On Sun, 16 Oct 2011 10:02:30 -0700, Jean-Christophe wrote:

> On 16 oct, 09:09, pozz
>
>> I received many warning messages of the type
>> * *W1207C: conditional expression is constant: dowhile
>> Those messages refer to multi-instructions macros defined as below:
>> * *#define MACRO(a, b) *do { foo(a); bar(b); } while(0)

>
> Why not
>
> #define MACRO(a,b) { foo(a); bar(b); }


The do...while construction requires a semicolon (i.e. will generate an error,
message if it's lacking), your construction doesn't. That's the usual reason to
do it that way.

-------------------------------------------------------------------------------
___________________________
< My vaseline is RUNNING... >
---------------------------
\
\
___
{~._.~}
( Y )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------


Keith Thompson 10-16-2011 08:49 PM

Re: Multi-instructions macro and compiler warning
 
Jean-Christophe <5.d@free.fr> writes:
> On 16 oct, 09:09, pozz
>> I received many warning messages of the type
>> * *W1207C: conditional expression is constant: dowhile
>> Those messages refer to multi-instructions macros defined as below:
>> * *#define MACRO(a, b) *do { foo(a); bar(b); } while(0)

>
> Why not
>
> #define MACRO(a,b) { foo(a); bar(b); }


See question 10.4 of the comp.lang.c FAQ, <http://c-faq.com/>.

The problem is that if you write:

if(cond)
MACRO(arg1, arg2);
else
/* some other code */

you get a syntax error due to the extra semicolon. The
do /* ... */ while (0)
idiom avoids that problem.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ian Collins 10-16-2011 09:00 PM

Re: Multi-instructions macro and compiler warning
 
On 10/17/11 09:49 AM, Keith Thompson wrote:
> Jean-Christophe<5.d@free.fr> writes:
>> On 16 oct, 09:09, pozz
>>> I received many warning messages of the type
>>> W1207C: conditional expression is constant: dowhile
>>> Those messages refer to multi-instructions macros defined as below:
>>> #define MACRO(a, b) do { foo(a); bar(b); } while(0)

>>
>> Why not
>>
>> #define MACRO(a,b) { foo(a); bar(b); }

>
> See question 10.4 of the comp.lang.c FAQ,<http://c-faq.com/>.
>
> The problem is that if you write:
>
> if(cond)
> MACRO(arg1, arg2);
> else
> /* some other code */
>
> you get a syntax error due to the extra semicolon. The
> do /* ... */ while (0)
> idiom avoids that problem.


So do functions!

--
Ian Collins


All times are GMT. The time now is 07:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.