C 中的运算符优先级(!= 和 |)
Operator precedence in C (!= and |)
我最近更正了 C 程序中的一个错误:
if (foobar != FOO | BAR | BAZ)
正确的代码是
if (foobar != (FOO | BAR | BAZ))
根据 C operator precedence,显然 !=
优先于 |
。
我的问题是为什么是这样而不是相反?根据我的经验,我经常使用 a == b || a == c
或 d == (a | b | c)
,但从不使用 a == b | c == d
。
这个选择背后的逻辑是什么?
这是有历史原因的,引自丹尼斯里奇:
“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.”
Dennis Ritchie 的 Development of the C Language 涵盖了这个和其他奇怪的历史文物。
基本上,语言最初没有 &&
和 ||
运算符——只有 &
和 |
,所以你 会 写类似 a == b | c == d
的东西。优先规则就是基于此设计的。
后来又增加了短路运算符,但没有修改旧运算符的优先级。
我最近更正了 C 程序中的一个错误:
if (foobar != FOO | BAR | BAZ)
正确的代码是
if (foobar != (FOO | BAR | BAZ))
根据 C operator precedence,显然 !=
优先于 |
。
我的问题是为什么是这样而不是相反?根据我的经验,我经常使用 a == b || a == c
或 d == (a | b | c)
,但从不使用 a == b | c == d
。
这个选择背后的逻辑是什么?
这是有历史原因的,引自丹尼斯里奇:
“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.”
Dennis Ritchie 的 Development of the C Language 涵盖了这个和其他奇怪的历史文物。
基本上,语言最初没有 &&
和 ||
运算符——只有 &
和 |
,所以你 会 写类似 a == b | c == d
的东西。优先规则就是基于此设计的。
后来又增加了短路运算符,但没有修改旧运算符的优先级。