Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any operator in c which is both unary and binary?

Is there any operator in c which is both unary and binary ? This question was asked in one of the interview.

like image 597
sarigehalli Avatar asked Dec 01 '22 19:12

sarigehalli


2 Answers

The asterisk (*) can be used for dereferencing (unary) or multiplication (binary).

The ampersand (&) can be used for referencing (unary) or bitwise AND (binary).

The plus/minus signs (+/-) can be used for identity/negation (unary) or addition/subtraction (binary).

But, as others pointed out, those are symbols shared by different operators. Each of those operators have only one n-arity.

like image 84
humodz Avatar answered Dec 10 '22 22:12

humodz


No, there isn't. Every operator is either unary, binary, or ternary.

Some unary and binary operators happen to use the same symbol:

  • * for dereference and multiplication
  • - for negation and subtraction
  • + for identity and addition
  • & for address-of and bitwise "and"

But unary and binary * are still distinct operators that happen to be spelled the same way.

like image 22
Keith Thompson Avatar answered Dec 10 '22 22:12

Keith Thompson