So the operator precedence of the ternary operator in C
seems truly bizarre to me. Case in point:
#include
int main ()
{
int i=5
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.