Comma operator

Discussion in 'C Programming' started by grid, Jun 8, 2005.

  1. grid

    grid Guest

    Hi,
    I need some clarifications on how the comma operator is used to return
    values from function like macros.I saw a typical implementation as :

    #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 , 0 )

    How is the 0 after the comma operator used to return 0 on success of the
    macro function ?

    TIA
     
    grid, Jun 8, 2005
    #1
    1. Advertising

  2. grid

    S.Tobias Guest

    grid <> wrote:

    > I need some clarifications on how the comma operator is used to return
    > values from function like macros.I saw a typical implementation as :


    > #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 , 0 )


    > How is the 0 after the comma operator used to return 0 on success of the
    > macro function ?


    `sigfillset' is not a function, it's a "function-like macro", but this
    detail is not very important here. A macro does not return anything,
    it gets expanded. In the above case, it gets expanded into a comma
    expression, whose value is _always_ 0, regardless whether the first
    operand expression succeeded or not.

    The above code may be some sort of optimization (macro expansion
    in place of function call). `sigfillset' is probably one of many
    "functions", which return 0 on success. Since setting a bit
    pattern cannot ever fail, we can always "return" 0.

    Example:
    int error;
    error = sigfillset(/*...*/);
    if (error) abort();
    error = sigfooset(/*...*/);
    if (error) abort();
    error = sigbarset(/*...*/);
    if (error) abort();
    /* etc... */

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Jun 8, 2005
    #2
    1. Advertising

  3. grid

    Zoran Cutura Guest

    grid <> wrote:
    > Hi,
    > I need some clarifications on how the comma operator is used to return
    > values from function like macros.I saw a typical implementation as :
    >
    > #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 , 0 )
    >
    > How is the 0 after the comma operator used to return 0 on success of the
    > macro function ?


    Actually it allways returns 0. The point is that the comma-operator is
    of such low precedence that the left operand of it is evaluated first,
    so in the above case the assignement to *ptr is done and thereafter the
    evaluation of the right hand operator of the ',' is done and becomes the
    recult of the expression. So if it is meant to return 0 on success one
    could rely on sigfillset to never fail.

    OTOH, if ptr is not a valid pointer anything might happen.

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
     
    Zoran Cutura, Jun 8, 2005
    #3
    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. Paul Davis
    Replies:
    7
    Views:
    601
    Paul Davis
    Jul 17, 2003
  2. Derek
    Replies:
    6
    Views:
    378
    Old Wolf
    Apr 13, 2004
  3. Koster

    Yet another comma operator question

    Koster, Jan 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    384
    Old Wolf
    Jan 18, 2004
  4. Michael Scarlett

    the whys of the comma operator

    Michael Scarlett, Jun 10, 2004, in forum: C Programming
    Replies:
    8
    Views:
    318
    Michael Scarlett
    Jun 19, 2004
  5. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    495
    Barry Schwarz
    Feb 8, 2005
Loading...

Share This Page