What do the ?
and :
signify here?
#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )
This is a ternary operator (also available in C, to which Objective C is a superset, and other languages that borrowed from it).
The expression before ?
is evaluated first; if it evaluates to non-zero, the subexpression before :
is taken as the overall result; otherwise, the subexpression after the colon :
is taken.
Note that subexpressions on both sides of :
need to have the same type.
Also note that using macro for calculating MAX
may produce unexpected results if arguments have side effects. For example, MAX(++a, --b)
will produce a doubled side effect on one of the operands.
As Kjuly mentioned it should be greater than sign, It's just an if statement.
(a > b) ? a : b
If a
is greater than b
then a
will be returned from MAX(a,b)
function or if b
is greater then if statement will be false and b
will be returned.
The ternary (conditional) operator in C
Check Evan's answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With