c - C 中的运算符优先级(!= 和 |)

标签 c operator-precedence

<分区>

我最近纠正了一个 C 程序中的错误:

if (foobar != FOO | BAR | BAZ)

正确的代码是

if (foobar != (FOO | BAR | BAZ))

根据C operator precedence显然 != 优先于 |

我的问题是为什么是这样而不是相反?根据我的经验,我会经常使用 a == b || a == cd == (a | b | c),但绝不是 a == b | c == d

这个选择背后的逻辑是什么?

最佳答案

这是有历史原因的,引用 Dennis Ritchie 的话:

“Early C had no separate operators for & and && or | and ||. Instead it used the notion (inherited from B and BCPL) of ‘truth-value context': where a Boolean value was expected, after ‘if‘ and ‘while‘ and so forth; the & and | operators were interpreted as && and || are now; in ordinary expressions, the bit-wise interpretations were used. It worked out pretty well, but was hard to explain. (There was the notion of ‘top-level operators’ in a truth-value context.) “The precedence of & and | were as they are now.

Primarily at the urging of Alan Snyder, the && and || operators were added. This successfully separated the concepts of bit-wise operations and short-circuit Boolean evaluation. However, I had cold feet about the precedence problems. For example, there were lots of programs with things like: if (a==b & c==d) …

“In retrospect it would have been better to go ahead and change the precedence of & to higher than ==, but it seemed safer just to split & and && without moving & past an existing operator.”

关于c - C 中的运算符优先级(!= 和 |),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37682090/

相关文章:

c - 如何使用 valgrind 查看 c 中函数的堆和堆栈使用情况?

自定义 Doxygen html 输出

c++ - c++ 标准是否指定了运算符&&(内置)的评估顺序?

c++ - 将自定义数组传递给函数的问题 (c++)

c++ - () 的优先级不大于 &&

c - 为什么在 C 的宏中使用循环而不是 block

c - 从 C 中的函数返回字符串指针

c - 如何初始化外部RAM中的变量?

c++ - Turbo C++ 和 GCC(在 Windows 上使用代码块)以不同方式评估相同的三元表达式

c++ - 重载运算符的评估顺序 |?