Computer Science, asked by vaishnavo8, 10 months ago

difference between unary and binary opreater​

Answers

Answered by JuveriyaF
6

Binary operator requires two operands and Unary operator requires one operand. Take this example. Here, in -5, the minus sign is unary operator and it requires one operand which is 5 here. The minus sign between 5 and 6 is binary operator which requires two operands(-5 and 6 here).

Answered by Shazzad
2

Answer:

Binary operator requires two operands and Unary operator requires one operand.

Take this example.

-5-6=-11

Here, in -5, the minus sign is unary operator and it requires one operand which is 5 here. The minus sign between 5 and 6 is binary operator which requires two operands(-5 and 6 here). The minus sign in -11 is also unary operator.

The C compiler differentiates unary and binary signs using operator precedence and associativity. Here, unary minus has higher precedence than binary minus and has right-left associativity. Binary minus has left-right associativity.

So, there is a minus in this code and the compiler need to decide which minuses are unary and which minuses are binary. Since unary minus is higher priority, let's assume all minuses as unary and check if any ambiguity arises. Unary minus is right to left associativity. So, -11 is first evaluated and that minus is taken as unary since it doesn't rise any ambiguity (If we take minus in -11 as binary, then it needs two operands each on both the sides. Here, 11 can be one operand but = is not an operand and thus ambiguity arises. Hence it is concluded as unary.)

Then going on left side, the minus between -5 and 6 is binary (If we take that minus as unary, then -6 is a whole operand. Before -6 there is a -5 which is another operand. Two operands cannot be close together without any operator. Because of this ambiguity, it is concluded as binary).

Then we have -5. We conclude this minus with unary since binary requires two operands each on both sides and we don't have one on left side.

Explanation:

Similar questions