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.