by Dinesh Thakur Category: Operator

The conditional expression operator (? :) is the only ternary operator in the C language. The conditional selection operator(? :) is more convenient to use than if-else provided there are only two options to choose from. It takes three operands and is used to evaluate one of the two alternative expressions depending on the outcome of a test expression as shown below.

test_expr ? exprl : expr2

Here, test_expr is followed by a question mark(?), is the test expression and expr1 and expr2 separated by a colon (:)  are the alternative expressions, only one of which is evaluated, depending on the outcome of test_expr. Observe how the question mark (?) and the colon (:) separate these expressions. If test_ expr evaluates as true (i. e., non-zero), the value of the entire expression is equal to the value of expr 1; otherwise, it is equal to the value of expr2. Consider, for example, the expression a< b? a : b. The value of this expression 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 be used 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 in a conditional expression are usually simple.

3. The else clause in an if statement can be omitted. This 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);
 
}

What is conditional expression operator(ternary operator)?



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.