0

what are unary, binary and tertiary operators?

18th Jun 2016, 12:43 PM
Sakshi Jain
Sakshi Jain - avatar
4 odpowiedzi
+ 6
Ok first let's set the ground: operators are functions. Functions can have a number of parameters. In context of operators parameters are called operands. *Unary* operators only have *one* operand. *Binary* operators have *two* operands. *Tertiary* operators have *three* operands. Unary operators in C++ are (not complete list): +, -, ++, -- or ! as you can use them like this: int a = 10; int b = +a + (-a); a++; ++a; --b; b--; bool c = !true; In this example, I've already used binary operators, as + and = are binary operators. Other binary operators are &&, ||, ::, ., |, & etc. (I leave out the example as this is the most common group of operators in C++ and I cheated and put in not just one but two binary operators in the unary operator example above. :-) ) The only ternary / tertiary operator in C++ is ?: that is a short-cut to an if-else as in int a = true ? 5 : 10; Makes a become 5. If the expression before ? is evaluated to false, the alternative after the : is assigned. Only use it for short and simple assignments as if-else is more readable when the true and false case get more complicated.
18th Jun 2016, 2:55 PM
Stefan
Stefan - avatar
+ 1
unary operator requires 1 operand for operation while binary requires 2 and tertiary requires 3
22nd Jun 2016, 4:17 PM
gandhiyash
0
thank you for the answer 😊
18th Jun 2016, 3:13 PM
Sakshi Jain
Sakshi Jain - avatar
0
No problem, I'm glad I could help. :-)
19th Jun 2016, 1:42 AM
Stefan
Stefan - avatar