Search code examples
cconditional-statementsternary-operatorternaryoperator-precedence

Unexpected Result, Ternary Operator in Gnu C


So the operator precedence of the ternary operator in C seems truly bizarre to me. Case in point:

#include <stdio.h>

int main ()
{
   int i=5;
   int j=6;
   int k=7;
   printf("A: %d\n", i+j+(k!=7)?1:11); //prints 1
   printf("B: %d\n", i+j+((k!=7)?1:11)); //prints 22
   return 0;
}

This seems similar to the question here:
C++ ternary conditional and assignment operator precedence
Ternary operator evaluation order

As a clarification, I understand that the parentheses make it work, as my comments in my original post indicated...

I'm just wondering why the language authors would pick an evaluation method that is so likely to trick people up, when the first statement seems like it could be formatted compiler-wise to be valid.

But those question deals with operators on the left-hand side or within class members, where as this weird behavior occurs on the RHS.


Solution

  • What is weird here? The first part is interpreted as:

    (11 + (k != 7)) ? 1 : 11
    

    and the second is interpreted as

     11 + ((k !=7) ? 1 :11)
    

    The first is caused by the precedence rules (binary arithmetic has higher precedence than the ternary operator) and the second circumvents the precedence rules through grouping the expression with parenthesis.

    Your edit asks for the reasons and one can usually only guess at those unless someone on the C committee who was present at the time comes around to help. My guess would be that it is much more common to use a complex expression and ask for its truth value than using the ternary operator to determine the value of an expression in arithmetic. Something like this comes to mind:

    return (froble() + 3) == 0 ? 23 : 5; // parens for sanity but works without
    

    if this would be interpreted as return (froble() + 3) == 5; I would be truly shocked.