where is different???

Discussion in 'C Programming' started by wende, Aug 14, 2003.

  1. wende

    wende Guest

    1.
    #define MA \
    ....
    {
    i = j;
    }
    ....


    2
    #define MA \
    ....
    do{
    i = j;
    }while(0);



    thanks for your answer...
    wende, Aug 14, 2003
    #1
    1. Advertising

  2. wende

    Kevin Easton Guest

    wende <> wrote:
    > 1.
    > #define MA \
    > ...
    > {
    > i = j;
    > }
    > ...
    >
    >
    > 2
    > #define MA \
    > ...
    > do{
    > i = j;
    > }while(0);


    The usual idiom is to leave off the trailing semicolon - and it's only
    useful with multiple-statement macros, so it is this:

    #define MA \
    { \
    i = j; \
    j = k; \
    }

    versus this:

    #define MA \
    do { \
    i = j; \
    j = k; \
    } while (0)

    With the former definition, this code:

    if (test)
    MA;
    else
    puts("no");

    is an error, but with the latter definition it is not. This is because
    MA; is two statements with the former definition and one statement with
    the latter.

    - Kevin.
    Kevin Easton, Aug 15, 2003
    #2
    1. Advertising

  3. wende

    Alex Guest

    Kevin Easton <> wrote:
    > wende <> wrote:


    <snip original>

    > The usual idiom is to leave off the trailing semicolon - and it's only
    > useful with multiple-statement macros, so it is this:


    > #define MA \
    > { \
    > i = j; \
    > j = k; \
    > }


    > versus this:


    > #define MA \
    > do { \
    > i = j; \
    > j = k; \
    > } while (0)


    > With the former definition, this code:


    > if (test)
    > MA;
    > else
    > puts("no");


    > is an error, but with the latter definition it is not. This is because
    > MA; is two statements with the former definition and one statement with
    > the latter.



    With the first definition, this will work:

    if(test)
    MA
    else
    puts("no");

    However, the requirement of the trailing semicolon in the
    second definition is much more idiomatic.

    Alex
    Alex, Aug 15, 2003
    #3
  4. Groovy hepcat Kevin Easton was jivin' on Thu, 14 Aug 2003 23:40:06 GMT
    in comp.lang.c.
    Re: where is different???'s a cool scene! Dig it!

    >#define MA \
    >{ \
    > i = j; \
    > j = k; \
    >}
    >
    >versus this:
    >
    >#define MA \
    >do { \
    > i = j; \
    > j = k; \
    >} while (0)
    >
    >With the former definition, this code:
    >
    >if (test)
    > MA;
    >else
    > puts("no");
    >
    >is an error, but with the latter definition it is not. This is because
    >MA; is two statements with the former definition and one statement with
    >the latter.


    Piffle! They're both perfectly legal. You've never heard of a null
    statement? Look that up in your C manual.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Aug 18, 2003
    #4
  5. wende

    Kevin Easton Guest

    Peter "Shaggy" Haywood <> wrote:
    > Groovy hepcat Kevin Easton was jivin' on Thu, 14 Aug 2003 23:40:06 GMT
    > in comp.lang.c.
    > Re: where is different???'s a cool scene! Dig it!
    >
    >>#define MA \
    >>{ \
    >> i = j; \
    >> j = k; \
    >>}
    >>
    >>versus this:
    >>
    >>#define MA \
    >>do { \
    >> i = j; \
    >> j = k; \
    >>} while (0)
    >>
    >>With the former definition, this code:
    >>
    >>if (test)
    >> MA;
    >>else
    >> puts("no");
    >>
    >>is an error, but with the latter definition it is not. This is because
    >>MA; is two statements with the former definition and one statement with
    >>the latter.

    >
    > Piffle! They're both perfectly legal. You've never heard of a null
    > statement? Look that up in your C manual.


    How about *you* look up the syntax of the if statement in *your* C
    manual?

    The expansion of the given if statement using the former definition of
    MA is:

    if (test)
    {
    i = j;
    j = k;
    };
    else
    puts("no");

    Which isn't legal. There can only be *one* statement between the test
    and else part of an if statement.

    - Kevin.
    Kevin Easton, Aug 19, 2003
    #5
    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. Tigerhillside
    Replies:
    3
    Views:
    483
    Joe Smith
    Feb 23, 2004
  2. AD
    Replies:
    1
    Views:
    1,028
    Francis Shanahan
    Aug 23, 2003
  3. Dotnetjunky
    Replies:
    0
    Views:
    374
    Dotnetjunky
    Aug 25, 2003
  4. Kevin Spencer
    Replies:
    0
    Views:
    422
    Kevin Spencer
    Aug 25, 2003
  5. chanmmn
    Replies:
    2
    Views:
    318
    Gawel
    Sep 7, 2004
Loading...

Share This Page