associtiavity of ternary operator

Posted on 2004-11-19
Last Modified: 2010-04-15
hi exeprts.
can anyone pleae give me an example showing the Right to left associativity of ternary operator.
Question by:Gupi
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Here is a case you can analyze:

    if ((a==b) ? 1 : 2)

    won't be the same as:

    if (a == b ? 1 : 2)
    LVL 15

    Expert Comment

    a ? b : c ? d : e;

    Right-to-left associativity says this is interpreted as

    a ? b : (c ? d : e);


    (a ? b : c) ? d : e;
    LVL 3

    Expert Comment


    Those 2 expressions evaluate the same...

    == has a higher precedence than ?:, and so it evaluates first regardless of parentheses.


    a == b ? 0 : 1


    a == ( b ? 0 : 1 )

    are different (since the parentheses force b ? 0 : 1 to be evaluated before ==)


    Author Comment

    hi. thanks all.
    lot of answers.
    but i am actually interested in an example where
    a ? b : c ? d : e;
    if intrepreted as  a ? b : (c ? d : e);
    will give different answer than if interpreted as  (a ? b : c) ? d : e;

    just like 100/20/2 will give different ans if implemented as 100/(20/2) or (100/20) /2

    waiting for a good easy to understand example.
    LVL 16

    Accepted Solution

    What do you want in the example? You've given a good one yourself with a ? b : c ? d : e;

          x = 0; printf("%d",x==0?0:x==1?1:2);
          x = 1; printf("%d",x==0?0:x==1?1:2);
          x = 2; printf("%d",x==0?0:x==1?1:2);

          x = 0; printf("%d",x==0?0:(x==1?1:2));
          x = 1; printf("%d",x==0?0:(x==1?1:2));
          x = 2; printf("%d",x==0?0:(x==1?1:2));

          x = 0; printf("%d",(x==0?0:x==1)?1:2);
          x = 1; printf("%d",(x==0?0:x==1)?1:2);
          x = 2; printf("%d",(x==0?0:x==1)?1:2);



    showing that the first set is functionally the same as the second and therefore the third is the non-default option.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now