() 的优先级最高,为什么会短路?
int a = 1, b = 0;
(--a)&&(b++);
为什么 (b++) 仍然短路?
最佳答案
我不觉得“短路”这个词特别有用。
不如说&&
和 ||
有从左到右的评估顺序。 C 标准 (6.5.13) 实际上很好地解释了这一点:
the && operator guarantees left-to-right evaluation ...
If the first operand compares equal to 0, the second operand is not evaluated.
就是这样。左操作数
--a
评估为 0。What is the difference between operator precedence and order of evaluation?
在这种情况下,重要的是评估顺序。运算符优先级仅保证表达式按预期解析 - 哪些操作数“粘合”到哪个运算符。
关于c - () 的优先级最高,为什么会短路?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66474877/