+ 1
Difference between unary and binary operator?
+,- why they are called both unary and binary.And why other operators are not?
2 odpowiedzi
+ 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
+ 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)