简体   繁体   中英

Unary operators and ++ or --

Why in the C specification only the prefix increment and decrement operators are summarized as Unary operators?

In fact the postfix increment and decrement operators are summarized into the section Postfix operators.

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. 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 .

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:

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++) ).

@Pascal Cuoq answered why they are in different groups: it is because they have different precedence and for no other reason. 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:

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

This table is to blame for a lot of evil in the world. K&R originally listed "everything unary" as one operator group. 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. Out of the two top groups in the K&R table, they made three. 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.

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. Mathematically, they are of course unary.

Firstly, Postfix operators are left-associative, and Unary operators are right-associative.

Secondly, Postfix operators have higher precedence than Unary operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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