The conditional operator is a special operator in C that provides a short way to choose one of two expressions based on a condition. It is also called the ternary operator because it works with three operands.
condition ? expression1 : expression2.
If the condition is true, expression1 is selected. Otherwise,
expression2 is selected.
The conditional operator allows a program to select one of two expressions depending on whether a condition is true or false.
int age = 20;
char *result = (age >= 18) ? "Adult" : "Minor";
printf("%s", result);
Output:
Adult
The conditional operator is called the ternary operator because it operates on three operands:
condition ? true_expression : false_expression;
condition ? expression1 : expression2;
The question mark ? separates the condition from the true
expression, while the colon : separates the true and false
expressions.
int number = 10;
int result = (number > 5) ? 100 : 200;
The condition number > 5 is true, so 100 is
selected and assigned to result.
result = 100
int age = 20;
int result = (age >= 18) ? 1 : 0;
printf("%d", result);
Output:
1
Because the age is at least 18, the first expression is selected.
int a = 10;
int b = 20;
int larger = (a > b) ? a : b;
printf("%d", larger);
Output:
20
The operator selects the larger value.
int a = 10;
int b = 20;
int smaller = (a < b) ? a : b;
printf("%d", smaller);
Output:
10
int marks = 75;
char *result = (marks >= 40) ? "Pass" : "Fail";
printf("%s", result);
Output:
Pass
The conditional operator selects one of the two string literals.
int number = 7;
char *result = (number % 2 == 0) ? "Even" : "Odd";
printf("%s", result);
Output:
Odd
The remainder operator is used to create the condition.
int number = -5;
char *result = (number >= 0) ? "Positive" : "Negative";
printf("%s", result);
Output:
Negative
This example treats zero as non-negative.
int age = 25;
int status;
status = (age >= 18) ? 1 : 0;
printf("%d", status);
Output:
1
int age = 16;
printf("%s", (age >= 18) ? "Adult" : "Minor");
Output:
Minor
The selected expression is passed directly to printf().
Using if-else:
if (age >= 18)
{
result = 1;
}
else
{
result = 0;
}
Using the conditional operator:
result = (age >= 18) ? 1 : 0;
The conditional operator can make a simple two-choice assignment more compact.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("%s", (number % 2 == 0) ? "Even" : "Odd");
return 0;
}
The program takes a number and directly displays whether it is even or odd.
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
printf("%s", (marks >= 40) ? "Pass" : "Fail");
return 0;
}
The condition checks whether the marks are at least 40.
#include <stdio.h>
int main()
{
int a = 50;
int b = 80;
int max = (a > b) ? a : b;
printf("Maximum = %d", max);
return 0;
}
Output:
Maximum = 80
#include <stdio.h>
int main()
{
int a = 50;
int b = 80;
int min = (a < b) ? a : b;
printf("Minimum = %d", min);
return 0;
}
Output:
Minimum = 50
float a = 10.5f;
float b = 20.5f;
float result = (a > b) ? a : b;
printf("%.2f", result);
Output:
20.50
The conditional operator can also select between floating-point values.
char grade1 = 'A';
char grade2 = 'B';
char result = (grade1 < grade2) ? grade1 : grade2;
printf("%c", result);
The operator can select one of two compatible character values.
A conditional operator can be placed inside another conditional expression. This is called a nested conditional operator.
int marks = 75;
char *grade =
(marks >= 80) ? "A" :
(marks >= 60) ? "B" :
(marks >= 40) ? "C" : "F";
printf("%s", grade);
Output:
B
Nested conditional expressions should be used carefully because too many levels can make code difficult to read.
int age = 25;
int marks = 70;
char *result =
(age >= 18 && marks >= 40) ? "Eligible" : "Not Eligible";
printf("%s", result);
Logical and relational operators can be used inside the condition.
int value = 0;
char *result = value ? "True" : "False";
printf("%s", result);
Output:
False
In a condition, zero is treated as false.
int value = 10;
char *result = value ? "True" : "False";
printf("%s", result);
Output:
True
In a condition, a nonzero value is treated as true.
int price = 500;
int discount = 1;
int finalPrice = discount ? price - 50 : price;
printf("Final Price = %d", finalPrice);
Output:
Final Price = 450
The selected expression becomes the value assigned to
finalPrice.
int number = -15;
int absolute = (number < 0) ? -number : number;
printf("%d", absolute);
Output:
15
The operator chooses the appropriate expression based on whether the number is negative.
? symbol.: symbol.? with other operators.if-else
statement would be clearer.int a = 10;
int b = 20;
int result = (a > b) ? a : b;
printf("%d", result);
Parentheses around the condition are commonly used to make the condition easy to read.
The entire conditional expression can also be placed inside a larger expression when appropriate.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
int maximum = (a > b) ? a : b;
int minimum = (a < b) ? a : b;
printf("Maximum = %d\n", maximum);
printf("Minimum = %d\n", minimum);
return 0;
}
This program uses the conditional operator to find both the maximum and minimum of two numbers.
Using if-else:
int number = 10;
int result;
if (number > 0)
{
result = 1;
}
else
{
result = 0;
}
Using conditional operator:
int number = 10;
int result = (number > 0) ? 1 : 0;
Both approaches can produce the same result. The conditional operator is particularly useful for short expressions with two alternatives.
Practice the following programs:
Practice simple conditions first. Once you understand the basic syntax, you can use the conditional operator with calculations and multiple conditions.
condition ? expression1 : expression2.if-else statement may be clearer.Question: Which operator is known as the ternary or conditional operator in C?