简体   繁体   中英

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.

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.

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.

What I think only . operator is both unary and binary in C (not specified in standard):

\n

. :- Unary: In designators of structures- {.meber1 = x, .member3 = z} (C99 and latter). Binary: Accessing structure members.


There is no operator in C which is unary and binary as well.
Symbols , like + , - , * and & , are used as unary and binary operators but then these symbols are treated as different operators :

  1. + , - Unary: i = -1 j = +1 . Binary: i = i+1 , j = j+1
  2. * Unary: Dereference operator. Binary: Multiplication operator.
  3. & Unary: Reference operator. Binary: Bitwise AND operator.

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