Search code examples
cbnfunary-operatorbinary-operators

In C BNF, UnaryOperator ::= ( "&" | "*" | "+" | "-" | "~" | "!" ). Why / and % are excluded in UnaryOperator?


In C BNF, MultiplicativeExpression and UnaryOperator are defined like the following:

MultiplicativeExpression ::= CastExpression ( ( "*" | "/" | "%" ) MultiplicativeExpression )?
UnaryOperator ::= ( "&" | "*" | "+" | "-" | "~" | "!" )

Are / and % defined in MultiplicativeExpression?


Solution

  • According to wikipedia

    a unary operation is an operation with only one operand..

    So, the operators which needs or works on only one operand, are unary operators.

    % and / definitely needs two operands, so they are not unary operators.

    In case, you're wondering about the presence of + and -, they are unary positive and negative operators (unary arithmetic operators), not addition and subtractions.

    Quoting C11, chapter §6.5.3.3

    The result of the unary + operator is the value of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.

    and

    The result of the unary - operator is the negative of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.