Every developer must know about operators. Operators are symbols that perform some operations on operands. In the C language, we have an efficient ternary operator. This article aims to understand the Ternary Operator in the C language. In this post, we will dive deep into this operator.
What is a Ternary Operator in C language?
A Ternary Operator is a condition operator in the C language, also known as a conditional operator. It has three arguments and provides a shorter way to code an if-else statement. It is used extensively in programming, where we need to write short conditional statements.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Syntax of Conditional /Ternary Operator in C language
The conditional operator form is:
variable = Expression1 ? Expression2 : Expression3;
The syntax can also be in this form
variable = (condition)? Expression2 : Expression3;
Syntax can also be in this form
(condition) ? (variable = Expression2) : (variable = Expression3);
Working as Conditional/Ternary Operator in C
Let’s understand how Ternary Operators work in C language. The following program demonstrates conditional Ternary Operators in C language.
Program
#include <stdio.h>
int main() {
int a =500, b = 20;
int max;
max = (a > b) ? a : b;
printf("Maximum value is: %d", max);
return 0;
}
Output
Maximum value is: 500
In this program, we use the ternary operator to find the maximum between two numbers.

82.9%
of professionals don't believe their degree can help them get ahead at work.
Flow Chart for Ternary Operator in C language
Flow Chart of Ternary Operator in C language.
Start
|
Evaluate Condition
|
|__ Yes (Condition is true) –> Return Value_if_True –> End
|
|__ No (Condition is false) –> Return Value_if_False –> End
Basic Example of Ternary Operator
In this section, we will see the basic example of the Ternary operator, where we find the highest digits in three numbers.
The following program demonstrates the Ternary Operator
Program
#include <stdio.h>
int main() {
int a = 343, b = 12434;
int max = (a > b) ? a : b;
printf("The maximum value is %d", max);
return 0;
}
Output
The maximum value is 12434
Nested Ternary Operator
In this section, we will learn about the nested ternary operator in the C language and find the largest number in three variables.
The following program demonstrates the Nested Ternary Operator
Progarm
#include<stdio.h>
void main()
{
int a =30, b = 50, c = 100, larg;
larg = (a>b)?((a>c)?a:c):((b>c)?b:c);
printf("Largest number is: %d", larg);
}
Output
The largest number is: 100
Using Ternary Operator in Function Arguments
The ternary operator can also be used as a function argument to make your code more readable and compact.
The following program demonstrates the ternary operator in function arguments
Program
#include <stdio.h>
void find_evenAnd_Odd(int isEven) {
printf(isEven ? "Number is even\n" : "Number is odd\n");
}
int main() {
int num = 7;
find_evenAnd_Odd(num % 2 == 0);
return 0;
}
Output
Number is odd
In this example, we passed the number to check whether it was odd or even. If it is odd, return “Number is Odd;” otherwise, return “Number is even.”
Using Ternary Operator for Variable Assignment
The ternary operator can be used for efficient variable assignment. The following program demonstrates the variable assignment in the ternary operator.
Program
#include <stdio.h>
// Ternary Operator example in Variable Assignment in
int main() {
int grade = 14;
char *result = grade >= 50 ? "Pass" : "Fail";
printf("You %s the exam.\n", result);
return 0;
}
Output
You Fail the exam.
In this code, we are checking whether the student passes or fails using the ternary operator.
Using Ternary Operator with Arrays
Arrays can also use the ternary operator. The following program demonstrates arrays with a ternary operator.
Program
#include <stdio.h>
int main() {
int array[5] = {10, 20,30,50,90};
int index = 1;
printf("Value: %d\n", index < 2 ? array[index] : -1);
return 0;
}
Output
Value: 20
Difference Between Ternary Operator and If…else Statement in C language
The following table differentiates the Ternary Operator and If..else Statement
| Aspect | Ternary Operator | If…else Statement |
| Syntax | ‘Condition ? exp1 : exp2’ | if(condition) {exp1;} else {expr2}’ |
| Usage | Compact and concise | More verbose |
| Number of Conditions | Supports only one Condition | Supports multiple conditions |
| Number of expressions | This operator exactly has two expression | They have multiple expressions |
| Return Value | Returns one of two expression | No specific return value | Nesting | It can be nested | It can also be nested.
What is the ternary Operator in the C symbol?
Is it good practice to always use the ternary operator in C for conditional operations?
What is the advantage of the ternary operator in C language?
Are ternary operators faster?
Updated on January 21, 2025

