comma before ?:

Discussion in 'C++' started by Fraser Ross, Jan 18, 2007.

  1. Fraser Ross

    Fraser Ross Guest

    int
    a= 4,
    c= 0,
    x= 100,
    y= 200;
    c ? ++a, x : y ; // comma before ?:

    Should a be 5 at the end? If this is not an example of comma precedence
    over ?: can someone give one?

    Fraser.



    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    Fraser Ross, Jan 18, 2007
    #1
    1. Advertising

  2. Fraser Ross

    Ron Natalie Guest

    Fraser Ross wrote:
    > int
    > a= 4,
    > c= 0,
    > x= 100,
    > y= 200;
    > c ? ++a, x : y ; // comma before ?:
    >
    > Should a be 5 at the end? If this is not an example of comma precedence
    > over ?: can someone give one?
    >

    First off, anybody who uses ?: and discards the value should
    have their fingers broken. That's what if/else is for.
    C++ parsing is not strictly determined by operator precedence,
    the ? operator is a pretty good examle of that.

    The question isn't one of precedence anyhow. Precedence doesn't
    change the order and when things are executed, just how they are
    parsed. If c is false, the stuff before the colon is NOT evaluated
    and no side-effects occur.
     
    Ron Natalie, Jan 18, 2007
    #2
    1. Advertising

  3. Fraser Ross wrote:
    > int
    > a= 4,
    > c= 0,
    > x= 100,
    > y= 200;
    > c ? ++a, x : y ; // comma before ?:
    >
    > Should a be 5 at the end?


    No.

    > If this is not an example of comma
    > precedence over ?: can someone give one?


    It *is* an example of comma precedence over ?:. The last line
    is interpreted as if it were written

    c ? (++a, x) : y ;

    HOWEVER, since 'c' is 0, the parenthesised (here) expression is
    not evaluated. 'a' will be 4. Change 'c' to be initialised with
    non-zero value, and 'a' will get incremented.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 18, 2007
    #3
  4. Fraser Ross

    ralpe Guest

    Fraser Ross wrote:

    > int
    > a= 4,
    > c= 0,
    > x= 100,
    > y= 200;
    > c ? ++a, x : y ; // comma before ?:


    ++a, x means "increase a by one and return x".

    > Should a be 5 at the end?


    c is zero, so ++a, x is not executed.

    Ralpe
     
    ralpe, Jan 18, 2007
    #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. Jason Miles
    Replies:
    1
    Views:
    606
    J├╝rgen Exner
    May 28, 2004
  2. Ryan Moore

    add a comma

    Ryan Moore, Aug 27, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,248
    William F. Robertson, Jr.
    Aug 28, 2003
  3. Martin Pahlplatz

    server.mappath with comma in foldername?

    Martin Pahlplatz, Nov 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    525
    Martin Pahlplatz
    Nov 11, 2003
  4. Yama

    Comma Delimited

    Yama, Dec 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    Ken Cox [Microsoft MVP]
    Dec 16, 2003
  5. Rasool
    Replies:
    3
    Views:
    531
    mikeb
    Mar 3, 2004
Loading...

Share This Page