The Conditional Operator, also called a Ternary, is used in decision-making. In this C programming language, the conditional or ternary Operator returns the statement depending upon the given expression result.
The basic syntax of a Ternary or conditional Operator in this programming language is as shown below. From the below syntax, If the given test condition is true, then it returns statement1, and if it is false, statement2 will return.
Test_expression ? statement1: statement2
Ternary or Conditional Operator in C Example
In this program, we use the Conditional to find whether the person is eligible to vote or not. This ternary operator program allows the user to enter his or her age and assign the user entered integer value to the age variable.
If the user entered a value is 18 or above, the C Programming will print the first statement after the ? symbol ” You are eligible to Vote “.
If the user enters below 18, the second statement (which is after the : symbol) will print. ” You are not eligible to Vote “.
#include<stdio.h>
int main()
{
int age;
printf(" Please Enter your age here: \n ");
scanf(" %d ", &age);
(age >= 18) ? printf(" You are eligible to Vote ") :
printf(" You are not eligible to Vote ");
return 0;
}

Let us try with a different value as the age to explain the conditional Operator.
Please Enter your age here:
19
You are eligible to Vote
Can a C ternary operator have multiple conditions?
Yes. We can use Logical operators to combine multiple conditions inside a ternary operator. We can call it the shorter version of the if else statement.
In the following example, we used the conditional or ternary operator to check whether the person’s age is between 18 and 60. If true, print Adult; otherwise, print Not Adult message.
#include <stdio.h>
int main()
{
int age = 30;
const char *result = (age >= 18 && age < 60) ? "Adult" : "Not Adult";
printf("%s", result);
}
Adult
Nesting Ternary Operator in C
Similar to the else if statement, we can nest the ternary operator to check multiple conditions. However, it is safer to use the else if or nested if statements compared to the conditional operator.
#include <stdio.h>
int main()
{
int marks = 82;
char grade = (marks >= 90) ? 'A' : (marks >= 75) ? 'B'
: (marks >= 65) ? 'C'
: (marks >= 50) ? 'D'
: 'F';
printf("Grade = %c\n", grade);
return 0;
}
Grade = B
Ternary operator in C for three variables
We can use the ternary operator for three variables, and it is a classic example of the nested ternary operator.
In the following example, we use the ternary operator to find the largest number among three variables. It is the same as a nested if else statement.
#include <stdio.h>
int main()
{
int a = 50, b = 100, c = 90;
int largest = (a > b)
? ((a > c) ? a : c)
: ((b > c) ? b : c);
printf("Largest Among Three = %d\n", largest);
return 0;
}
Largest Among Three = 100