+ 1

Difference between unary and binary operator?

+,- why they are called both unary and binary.And why other operators are not?

15th Nov 2019, 4:44 PM
Nig
2 Réponses
+ 7
Unary operators work on a single operand.. Example ++ and -- operators. i++ , ++i , i-- and --i are some examples. Binary are the ones that work on two operands....(i.e. are surrounded by 2 operands, 1 on each side) Example, a+b, 1*5....etc. Now if we want to write a=a+1 Then we can write the same as a+=1. Here you can see that before equal sign, there is only one operand....hence, + is an unary as well as binary operator.. Same applies for - , * , / and % operators also
15th Nov 2019, 5:17 PM
Charitra
Charitra - avatar
+ 1
+ and - can be used to denote whether a number is positve or negative, and negation operates only on a single operand. * and / are not unary because *5 is just a syntax error, you need to use two operands, like 4 * 5. +4 (legal) 4+ (illegal) -4 (legal) 4- (illegal) *4 (illegal) 4* (illegal) /4 (illegal) 4/ (illegal)
15th Nov 2019, 7:48 PM
Rora