Unexpected Result, Ternary Operator in Gnu C

天涯浪子 提交于 2019-12-31 04:58:05

问题


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.


回答1:


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.




回答2:


One should pick a very high or very low precedence, and one or the other will be surprising to someone who makes the wrong assumption.

A useful reason for choosing low precedence is that it means that the operator functions like an if .. then .. else .. construct without any braces, which might mean less work for compiler writers (who might use the same code to handle both), and straightforward refactoring by coders who understand the precedence.

In practice, the language probably standardised whatever precedence was the most popular usage in code written in the pre-standardization era.



来源:https://stackoverflow.com/questions/9763483/unexpected-result-ternary-operator-in-gnu-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!