Unary Opeartions Inside Ternary Operator

Hi Guys ,
As per the operator precedence table, unary operators have a higher precedence over ternary operator,then in the below given example ,why this rule is not followed ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
   int  a = 1;
   int  b = 0;
   int  c = 9;
    
    int x = (a>1)? ++b :++c ;
    
    cout<<"\n Val of X = \t"<< x ;
    cout<<"\n Val of B = \t"<< b ;
    cout<<"\n Val of C = \t"<< c ;
    
    return 0;
}


The output being :
Val of X = 10
Val of B = 0
Val of C = 10.


what I infer from the output is first the condition is matched and based upon that pre-increment is done .
However ,pre -increment has a higher precedence over ternary operator ,so how does this thing is performed by the compiler ?
One thing I must agree that , unary operation is a part of the ternary operation ,does that means in such scenarios ,i.e where a higher precedence operator is a sub part of lower precedence, the compiler executes the lower precedence operation first .
Last edited on
The precedence rules don't say in which order things are evaluated.

First the expression (a>1) will be evaluated.
If it is true then ++b will be evaluated.
Otherwise ++c will be evaluated.
Last edited on
@ Peter 87 ,that is the query, for any operator there is "associativity " and order of preference ,they why ternary first and then unary ?
Last edited on
precedence determines parenthesis
a>1? ++b :++c
means
(a>1)? (++b) :(++c)
not
a>(1? (++b) : (++c))
because > has a higher precedence than ?:

same as *p++ means *(p++) not (*p)++
because postfix ++ has higher precedence than dereference *

1 + 2 + 3 * 4 means 1 + 2 + (3*4) not (1+2+3) * 4
because product * has higher precedence than addition +
1
2
3
4
5
a ? b : c + d
// could be
(a ? b : c) + d
// or 
a ? b : (c + d)

and precedence between ? : and + does determine whether the third operand of the ternary is c + d or c.

What (is in the operand), not when (it is evaluated).

However, the ternary is a conditional and operand of one of the branches won't be evaluated at all.

Same goes for the "lazy" && and ||, where the right operand won't be evaluated, if the left operand's value is sufficient to determine the boolean.
> for any operator there is "associativity " and order of preference ,they why ternary first and then unary ?

The C++ standard does not directly specify the precedence of operators; they are inferred from the specification of the grammar.

Re. the conditional operator: first ? second : third
Every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second or third expression.
http://eel.is/c++draft/expr.cond#1


Note that this is the C++ rule; the C rules are somewhat different.

For example, this is valid C++ , but invalid in C
1
2
3
4
5
int main()
{
    int a = 5, b = 0 , c = 0 ;
    a < 10 ? b = 2 : c = 8 ;
}

C++ http://coliru.stacked-crooked.com/a/2e2409e0b11a0a53
C http://coliru.stacked-crooked.com/a/d41f7883fbafd154
Thanks everybody for your explanations,I got it .

ne555 thx for using the word " parenthesis ",peter87 also tried the same thing ,but it just dint
struck me .


keskiverto and JLBorges thanks for the insights .
Topic archived. No new replies allowed.