简体   繁体   English

一元运算符和++或 -

[英]Unary operators and ++ or --

Why in the C specification only the prefix increment and decrement operators are summarized as Unary operators? 为什么在C规范中只将前缀增量和减量运算符概括为一元运算符?

In fact the postfix increment and decrement operators are summarized into the section Postfix operators. 事实上,post fi x递增和递减运算符被汇总到Postfix运算符部分。

I don't see the difference. 我没有看到差异。 Both operate on a sole operand. 两者都在一个操作数上运行。

REWRITTEN : I guess I mis-read the spec, as pointed out by @Lundin. REWRITTEN :正如@Lundin指出的那样,我想我错误地阅读了这个规范。 A better answer seems to be that the names used in this part of the standard don't make a lot of sense. 一个更好的答案似乎是标准的这一部分中使用的名称没有多大意义。

Of course both postfix and prefix increment/decrement are both just as unary as the other. 当然,后缀和前缀增量/减量都和另一个一样。 That's how they're used. 这就是他们的使用方式。 The fact that they have different priorities mean that they are put in different groups, and the groups have historical names which are a bit off. 它们具有不同优先级的事实意味着它们被放在不同的组中,并且这些组具有稍微偏离的历史名称。

You are referring to the C standard, of which one draft of one version is here . 您指的是C标准,其中一个版本的草稿就在这里

The reason why postfix inc/decrement are in a different section than prefix inc/decrement is that this part of the standard follows the grammar, and that in order to remove ambiguity, these operators are at different levels of the grammar: postfix inc / decrement与前缀inc / decrement不同的部分是因为标准的这一部分遵循语法,并且为了消除歧义,这些运算符处于语法的不同级别:

unary-expression: 
  postfix-expression
  ++ unary-expression
  -- unary-expression
  ...


postfix-expression:
  primary-expression
  ...
  postfix-expression ++
  postfix-expression --

This allows an expression such as ++ E ++ (while such an expression would never be valid anyway) to be parsed unambiguously as ++(E++) ). 这允许诸如++ E ++类的表达式(虽然这样的表达式永远不会有效)被明确地解析为++(E++) )。

@Pascal Cuoq answered why they are in different groups: it is because they have different precedence and for no other reason. @Pascal Cuoq回答了为什么他们在不同的群体中:这是因为他们有不同的优先权,没有其他原因。 It has little to do with whether they are actually unary or binary operators. 它与实际上是一元运算符还是二元运算符几乎没有关系。

The reason why the C uses confused names for the operator groups is most likely this classic, yet incorrect table: C使用操作符组的混淆名称的原因很可能是这个经典但不正确的表:

K&R 2nd edition page 48. K&R第2版第48页。 在此输入图像描述

This table is to blame for a lot of evil in the world. 这张桌子应该归咎于世界上许多邪恶。 K&R originally listed "everything unary" as one operator group. K&R最初将“所有一切”列为一个运营商集团。 But of course this is correct: it doesn't make sense for the postfix ++ -- to be grouped with the prefix operators. 但当然这是正确的:对于后缀++没有意义 - 与前缀运算符组合在一起。

As C was standardized, the committee likely realized that this table didn't make any sense, so they made additional groups. 由于C是标准化的,委员会可能意识到这个表没有任何意义,因此他们组建了更多的组。 Out of the two top groups in the K&R table, they made three. 在K&R表中的两个顶级组中,他们做了三个。 One group for primary expression (plain parenthesis () included there), one group for postfix ones and one group still called unary, likely because of historical reasons. 一个用于主要表达的组()包括那里的普通括号() ),一组用于后缀,一组仍称为一元,可能是由于历史原因。 A better name for the latter would no doubt have been prefix operators, but the C standard isn't exactly known to be rational. 后者的一个更好的名称无疑是前缀运算符,但C标准并不完全清楚是合理的。

I guess that it is just a question of structure of the standard. 我想这只是标准结构的问题。 The C11 standard has a special section for postfix operators, so they are not classified with the rest of infix and prefix operators. C11标准有一个专门用于后缀运算符的部分,因此它们不与其余的中缀和前缀运算符分类。 Mathematically, they are of course unary. 在数学上,它们当然是一元的。

Firstly, Postfix operators are left-associative, and Unary operators are right-associative. 首先,Postfix运算符是左关联的,而一元运算符是右关联的。

Secondly, Postfix operators have higher precedence than Unary operators. 其次,Postfix运算符的优先级高于一元运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM