Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C operator (?) and (:)

What do the ? and : signify here?

#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )
like image 388
stumped Avatar asked Nov 27 '22 03:11

stumped


2 Answers

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.

like image 75
Sergey Kalinichenko Avatar answered Jan 13 '23 03:01

Sergey Kalinichenko


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

like image 35
TeaCupApp Avatar answered Jan 13 '23 04:01

TeaCupApp