-4

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

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

Are / and % defined in MultiplicativeExpression?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
aheh
  • 57
  • 1
  • 6

3 Answers3

2

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Some characters are used for more than one operator. * is used for both the multiplication operator (which is a binary operator) and the address indirection operator (which is a unary operator). Thus, you can have an expression like

x = a * *p; // multiply a by what p points to

% and / do not have a similar use in unary expressions, which is why they don't appear in the list of unary operators.

& is another character that can be used as a unary operator (address-of) or a binary operator (bitwise and).

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

/ and % never exist in contexts that take just one operand so therefore they are never unary operators. As for the other operators given:

  1. & can mean address-of (as well as bitwise AND)
  2. * can mean pointer dereference (as well as multiplication).
  3. + and - can be unary plus / minus.
  4. ! and ~ are logical negation and bitwise complement.
Bathsheba
  • 231,907
  • 34
  • 361
  • 483