The conditional operator (?:) is a shorthand way of writing simple if-else statement. It evaluates a condition and returns one of two expressions based on whether the condition is true or false.
- Reduces code length by replacing simple
if-elsestatements with a single expression. - Improves code readability when performing straightforward conditional assignments.
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("Maximum = %d", max);
return 0;
}
Output
Maximum = 20
Syntax
The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;
Or the syntax can also be in this form
variable = (condition) ? Expression2 : Expression3;
Or syntax can also be in this form
(condition) ? (variable = Expression2) : (variable = Expression3);

It can be visualized into an if-else statement as:
if(Expression1){
variable = Expression2;
}else{
variable = Expression3;
}
Since the Conditional Operator '?:' takes three operands to work, hence they are also called ternary operators.
Note: The conditional (
?:) operator has one of the lowest operator precedences in C. Use parentheses when combining it with other operators to avoid unexpected results.
Working
The working of the conditional operator in C is as follows:
- Step 1: Expression1 is the condition to be evaluated.
- Step 2A: If the condition (Expression1) is True then Expression2 will be executed.
- Step 2B: If the condition (Expression1) is false then Expression3 will be executed.
- Step 3: Results will be returned.
Flowchart of Conditional/Ternary Operator in C
To understand the working better, we can analyze the flowchart of the conditional operator given below.

Examples
There are multiple examples some of them are:
Example 1: C Program to Store the greatest of the two Numbers using the ternary operator
// C program to find largest among two
// numbers using ternary operator
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d",
m, n)
: printf("n is greater than m that is %d > %d",
n, m);
return 0;
}
Output
m is greater than n that is 5 > 4
Example 2: C Program to check whether a year is a leap year using ternary operator
#include <stdio.h>
int main()
{
int yr = 1900;
(yr%4==0) ? (yr%100!=0? printf("The year %d is a leap year",yr)
: (yr%400==0 ? printf("The year %d is a leap year",yr)
: printf("The year %d is not a leap year",yr)))
: printf("The year %d is not a leap year",yr);
return 0;
}
//This code is contributed by Susobhan AKhuli
Output
The year 1900 is not a leap year
Advantages
The conditional (?:) operator provides a concise and efficient way to write simple conditional expressions.
- Reduces Code Length: Replaces simple
if-elsestatements with a single, compact expression. - Improves Readability: Makes straightforward conditional assignments easier to read and understand.
- Supports Expression-Based Programming: Can be used directly in assignments, function arguments, and return statements.
Limitations
Although the conditional operator simplifies simple conditions, it is not suitable for every programming scenario.
- Not Ideal for Complex Logic: Nested or complex conditional expressions can make the code difficult to read and maintain.
- Limited to Single Expressions: It cannot replace
if-elsewhen multiple statements need to be executed in each branch. - May Reduce Readability: Excessive use of nested ternary operators can make code confusing and error-prone.