Re: Multi-instructions macro and compiler warning

Discussion in 'C Programming' started by Jean-Christophe, Oct 16, 2011.

  1. 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); }
     
    Jean-Christophe, Oct 16, 2011
    #1
    1. Advertising

  2. 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 )
    ()~*~()
    (_)-(_)
    -------------------------------------------------------------------------------
     
    Kleuskes & Moos, Oct 16, 2011
    #2
    1. Advertising

  3. Jean-Christophe <> 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.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 16, 2011
    #3
  4. Jean-Christophe

    Ian Collins Guest

    On 10/17/11 09:49 AM, Keith Thompson wrote:
    > Jean-Christophe<> 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
     
    Ian Collins, Oct 16, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    2
    Views:
    9,556
    Alvin Bruney [ASP.NET MVP]
    Mar 16, 2005
  2. Michael B Allen

    Suppressing Compiler Warning About Tricky Macro

    Michael B Allen, Jul 22, 2007, in forum: C Programming
    Replies:
    6
    Views:
    363
    Barry Schwarz
    Jul 22, 2007
  3. Tom Anderson
    Replies:
    4
    Views:
    531
    Peter Flynn
    Dec 13, 2008
  4. Ian Collins

    Re: Multi-instructions macro and compiler warning

    Ian Collins, Oct 16, 2011, in forum: C Programming
    Replies:
    12
    Views:
    973
    Keith Thompson
    Oct 17, 2011
  5. ImpalerCore

    Re: Multi-instructions macro and compiler warning

    ImpalerCore, Oct 17, 2011, in forum: C Programming
    Replies:
    4
    Views:
    430
    Keith Thompson
    Oct 18, 2011
Loading...

Share This Page