USING CONDITIONAL OPERATOR
The conditional operator (?:) takes three operands. It tests the result of the first operand and then evaluates one of the other two operands based on the result of the first. Consider the following example:
E1 ? E2 : E3If expression E1 is nonzero (true), then E2 is evaluated, and that is the value of the conditional expression. If E1 is 0 (false), E3 is evaluated, and that is the value of the conditional expression. Conditional expressions associate from right to left. In the following example, the conditional operator is used to get the minimum of
x
andy
:a = (x < y) ? x : y; /* a = min(x, y) */There is a sequence point after the first expression (E1). The following example's result is predictable, and is not subject to unplanned side effects:
i++ > j ? y[i] : x[i];The conditional operator does not produce an lvalue. Therefore, a statement such as
a ? x : y = 10
is not valid.The following restrictions apply:
- The first operand must have a scalar type.
- One of the following must hold for the second and third operands:
- Both operands have an arithmetic type (the usual arithmetic conversions are performed to bring the second and third operands to a common type). The result has that type.
- Both operands have compatible structure or union types.
- Both operands have a type of
void
. - Both operands are pointers to qualified or unqualified versions of compatible types. The result has the composite type.
- One operand is a pointer and the other is a null pointer constant. The result has the type of the pointer that is not a null pointer constant.
- One operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of
void
. The result has the typepointer tovoid
.
To find greatest of three numbers in single line with efficient coding that yields u less time and space complexity
((a<b)&&(a<c))?a:((b<c)?b:c)