## C Conditional Operator

It is a ternary operator. They are represented by the symbols (? and :). They need three operands to perform any operation successfully. Expressions with conditional operator and operands (expressions) are collectively called the “conditional expression”.

Syntax: Test Expression? Operand 1: Operand 2.
Execution: Very first the test expression will be evaluated. If it gives any non-zero value that will be considered this statement is true then the control passes to operand 1 and the value of operand 1 will be evaluated, which in result will be the final value of this “conditional expression”.

If the test expression is false (gives zero) then the control passes to the operand 2 and the value of operand 2 will be evaluated, which in result will be the final value of this “conditional expression”. Now Let ‘u’ and ‘v’ are two variables with their respective values 2 and 5. i.e. u=2 and v=5. We will discuss a few examples and the value of variables u and v in those examples will be taken as 2 and 5 respectively.

Example: u>v? u:v;
Execution Explanation: First of all the statement (expression) u>v will be evaluated. Since in this exam, the statement is false hence the control will transfer to operand 2. Thus, The value of variable ‘v’ will be the value of the “conditional expression”.

Example: Let ‘a’ be another variable such that (a = u<v ? u:v).
Execution Explanation: Since the value of the given statement (u<v) is true. The control will transfer to operand 1 and the value of the variable ‘u’ will be the value of this condition expression (u<v ? u:v). Finally, this value will be stored in variable ‘a’.

Example: u<v? printf(“Banana”) : printf(“Apple”);
Execution Explanation: Since the value of the given statement (u<v) is true. The control will be transferred to operand 1 and the result of this “conditional expression” will be “Banana”.

```Example: C program to find the smaller of the two natural numbers.
#include<stdio.h>
main()
{
int u, v, s;
printf("Enter the values of u and v : ");
scanf("%d %d", &u, &v);
s = u<v ? u : v;
printf("Smaller of %d and %d is %d\n", u, v, s);
}
Output:
Enter the values of u and v: 5 9
Smaller of 5 and 9 is 5
```