Objective-C operator (?) And (:)

What do they mean ? and : here?

 #define MAX(a,b) ( ((a) > (b)) ? (a) : (b) ) 
+4
source share
4 answers

As KΓΌyli said, it should be more than a sign. This is just an if statement.

 (a > b) ? a : b 

If a greater than b , then a will be returned from the MAX(a,b) function MAX(a,b) or if b greater, then if the statement is false and b is returned.

Ternary (conditional) operator in C

Check Evan's answer

+9
source

This is a ternary operator (also available in C, for which Objective-C is a superset and other languages ​​that it has borrowed).

? expression before evaluated first ? ; if he evaluates a value other than zero, the subexpression before : taken as the overall result; otherwise, a subexpression after the colon is executed :

Note that the subexpressions on both sides : must be of the same type.

Also note that using a macro to calculate MAX may produce unexpected results if the arguments have side effects. For example, MAX(++a, --b) will lead to a double side effect for one of the operands.

+13
source

?: is a ternary operator. What does ((a) > (b)) ? (a) : (b) mean ((a) > (b)) ? (a) : (b) ((a) > (b)) ? (a) : (b) :

 if (a > b) return a else return b 

Confirm HERE .

+8
source

This is a legend symbol. a? b: c.

If a evaluates to true, then b, else c.

I believe this should be: #define MAX (a, b) (((a)> (b))? (A): (b))

so basically, if a is greater than b, then a, else b

+4
source

All Articles