0

Since the associativity of '?' is from right to left,any 2 consecutive '?' operators must be treated as such,Right?

Now,

int x=-1;
int y=x?x++?x:-1:1;

I expect this to be executed as:

int y = x ? (x++?x:-1) : 1;

Now since its being executed from right to left,when encountering the first '?' in the statement,x's value is 0 and the expression is as

int y= x? 0 : 1;

hence i expected y to be 1,but it shows Zero on my dev-cpp.Where am i wrong?

nikel
  • 3,402
  • 11
  • 45
  • 71

3 Answers3

2

You have the order of evaluation wrong. In a ? b : c, a is always evaluated first, then either b or c is evaluated.

I've marked up your example so that I can identify subexpressions:

            c
int y=x?x++?x:-1:1;
      a bbbbbbbb

(a) is evaluated, yielding -1, so (b) is evaluated. There, x++ is evaluated, yielding -1 again, so (c) is evaluated. At this point, x is 0.

Or, with more verbose, clearer code, it's as if you said:

int x = -1;
int y;
if (x != 0)
{
    int new_x = x + 1;
    if (x != 0)
    {
        y = new_x;
    }
    else
    {
        y = -1;
    }
}
else
{
    y = 1;
}
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • so, its just like normal conditional thingy....where is the associativity of ? being used....why shouldnt it be done as left to right as in a=b=c=5?? – nikel Dec 02 '11 at 06:37
  • 1
    I don't understand your question. Associativity specifies how multiple instances of an operator are grouped. However, it is irrelevant here: `a ? b ? c : d : e` cannot be parsed any way except `a ? (b ? c : d) : e` (there's absolutely no other way to form a valid expression). The specification states that in `a ? b : c`, `a` is evaluated first, then either `b` or `c` is evaluated. Thus, the behavior is as I describe in the answer. – James McNellis Dec 02 '11 at 06:44
  • got ya...usually read that in this way a=b=c=5..since associativity of = is right to left,c=5 is executed EARLIER...this led to a conception that associativity defines order of exec...its fine now,associativity defines grouping to form an entity....thanks:) – nikel Dec 02 '11 at 07:10
1

Operations:

Assign y to value = 
    if(x): --> x = -1, so true as it is non-zero
    {
      if(x): --> x = -1 ,so true as x will increment later due to post increment
       x= x +1; --> increment x, so x = 0 . This is the value assigned. So y = 0;
     else:
       -1
    }
    else: 
    {
      1
    }

Hope this helps!

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
  • in a=b=c=5; it is executed from left to right as a=(b=(c=5)) right...why doesn't the context of my question get executed from right to left?why using the very initial value of 'x' ? – nikel Dec 02 '11 at 06:32
0

The answer to your question is that in C/C++ int y = x ? (x++?x:-1) : 1; we will hit two sequence points at ?. Any update operations to variable with in a sequence point will be effective after that sequence is over. So lets look at our example in hand.

First sequence point is first ? from left.

x=-1; (Actual Value)
x=-1; (Value used in expression)
y=-1?(x++?x:-1):1;

Second sequence point is second ? from left. As mentioned above the update operations are effective after sequence so even though x++ is there the value used in this sequence is -1 and updated value will be used in following.

x=0; (Actual Value, bcoz of x++)
x=-1; (Value used in expression)
y=-1?x:-1;

Now it will be

x=0; (Actual Value)
x=0; (Value used in expression)
y=x;
y=0;

Hope this make sense now.

havexz
  • 9,550
  • 2
  • 33
  • 29