The conditional expression operator (?:) is the only ternary operator in C language. It takes three operands and is used to evaluate one of the two alternative expressions depending on the outcome of a test expression. The controlling condition of the ternary operator must evaluate to boolean, either true or false . If the condition is true , the ternary operator returns expr1 , otherwise it returns the expr2 .
Explanation: ?: = Question Mark Colon is also called C Ternary Operator
The Ternary operator is faster than if-else statement in its functioning. The ternary operator (?:) is more convenient to use than if-else provided there are only two options to choose. However, its syntax differs from if-else syntax. For example:
test_expr ? expr1 : expr2
Here, test_expr follow by a question mark(?), are the test expression, and expr1 and expr2 separated by a colon (:) are the alternative expressions, only one of which evaluate, depending on the outcome of test_expr.
Observe how the question mark (?) and the colon (:) separate these expressions. If test_expr considers as true (i. e., non-zero), the value of the entire expression is equal to the value of expr1; otherwise, it is equal to the value of expr2.
Consider, for example, the expression a< b? a: b. This expression’s value is either a or b, depending on whether the expression is true or false, respectively. Thus, the expression determines the smaller of the two numbers a and b.
The contained expressions may be of any such as arithmetic, relational, Boolean, assignment, etc. They may also be function calls or involved expressions containing function calls. We can also use a comma operator to write multiple expressions in place of sub-expressions. The conditional expression can use just like any other expression. Thus, we can use it as a part of more involved expressions, assign its value to a variable, or use it as an argument in a printf or other function call.
Observe that the conditional expression operator has right-to-left associativity and lower precedence than all other operators except the assignment and comma operators. Thus, while writing conditional expressions, the parentheses surrounding the contained expressions are often unnecessary unless we use assignment or comma operators in those expressions. However, to improve the readability, we can include complex expressions within parentheses, as shown below.
(test_expr) ? (exprl) : (expr2)
The conditional expression is a powerful facility that allows us to write very concise code. It is very similar to the if-else statement, and we can often replace it. However, there are some differences:
1. The if-else statement is more readable than the conditional expression but requires more space.
2. The if and else blocks can be any valid C statement, including any control statement or a compound statement that may contain hundreds of statements. However, we cannot use any C statement or a block in a conditional expression. Moreover, to keep the code readable, expressions expr1 and expr2 is a conditional expression are usually simple.
3. The else clause in an if statement can be omitted. It is not possible in conditional expressions.
Illustrates application of conditional selection operator(?:).
#include<stdio.h> void main() { int x, y, z, Max1,Max2,Min; clrscr(); printf("Enter three whole numbers:\n"); scanf( "%d %d %d",&x, &y, &z ) ; Min = x < y ? x : y; printf("Minimum of the first two numbers= %d\n" ,Min ); Max1 = (x > y? x : y); printf("Maximum of the first two numbers= %d\n" ,Max1); Max2 = (x> y ?x : y) > z ? (x>y ? x:y): z; printf("Maximum of the three numbers is= %d\n", Max2); }