Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Multi-instructions macro and compiler warning

Reply
Thread Tools

Re: Multi-instructions macro and compiler warning

 
 
Jean-Christophe
Guest
Posts: n/a
 
      10-16-2011
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); }
 
Reply With Quote
 
 
 
 
Kleuskes & Moos
Guest
Posts: n/a
 
      10-16-2011
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 )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-16-2011
Jean-Christophe <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-16-2011
On 10/17/11 09:49 AM, Keith Thompson wrote:
> Jean-Christophe<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Multi-instructions macro and compiler warning ImpalerCore C Programming 4 10-18-2011 06:52 PM
Re: Multi-instructions macro and compiler warning Ian Collins C Programming 12 10-17-2011 07:30 PM
Suppressing Compiler Warning About Tricky Macro Michael B Allen C Programming 6 07-22-2007 10:52 PM
warning C4267 and warning C4996 B. Williams C++ 17 10-27-2006 09:41 PM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM



Advertisments