Ternary Operator in C
Ternary Operator in C
Ternary Operator in C
The ternary operator (?:) in C is a type of conditional operator. The term "ternary"
implies that the operator has three operands. The ternary operator is often used to
put multiple conditional (if-else) statements in a more compact manner.
#include <stdio.h>
int main(){
int a = 10;
return 0;
}
Output
When you run this code, it will produce the following output −
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 1/7
6/16/24, 11:52 AM Ternary Operator in C
10 is Even
Change the value of "a" to 15 and run the code again. Now you will get the following
output −
15 is Odd
Example 2
The conditional operator is a compact representation of if–else construct. We can
rewrite the logic of checking the odd/even number by the following code −
#include <stdio.h>
int main(){
int a = 10;
if (a % 2 == 0){
printf("%d is Even\n", a);
}
else{
printf("%d is Odd\n", a);
}
return 0;
}
Output
10 is Even
Example 3
The following program compares the two variables "a" and "b", and assigns the one
with the greater value to the variable "c".
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 2/7
6/16/24, 11:52 AM Ternary Operator in C
#include <stdio.h>
int main(){
c = (a >= b) ? a : b;
return 0;
}
Output
When you run this code, it will produce the following output −
a: 100 b: 20 c: 100
Example 4
The corresponding code with if–else construct is as follows −
#include <stdio.h>
int main(){
if (a >= b){
c = a;
}
else {
c = b;
}
printf ("a: %d b: %d c: %d\n", a, b, c);
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 3/7
6/16/24, 11:52 AM Ternary Operator in C
Output
a: 100 b: 20 c: 100
Example 5
If you need to put multiple statements in the true and/or false operand of the
ternary operator, you must separate them by commas, as shown below −
#include <stdio.h>
int main(){
return 0;
}
Output
In this code, the greater number is assigned to "c", along with printing the
appropriate message.
a is larger a: 100 b: 20 c: 20
Example 6
The corresponding program with the use of if–else statements is as follows −
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 4/7
6/16/24, 11:52 AM Ternary Operator in C
return 0;
}
Output
a is larger
a: 100 b: 20 c: 100
First C checks if expr1 is true. If so, it checks expr2. If it is true, the result is expr3;
if false, the result is expr4.
If expr1 turns false, it may check if expr5 is true and return expr6 or expr7.
Example 1
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 5/7
6/16/24, 11:52 AM Ternary Operator in C
int main(){
int a = 15;
printf("a: %d\n", a);
(a % 2 == 0) ? (
(a%3 == 0)? printf("divisible by 2 and 3") : printf("divisible by 2 but not 3"))
:(
(a%3 == 0)? printf("divisible by 3 but not 2") : printf("not divisible by 2, not divisible by 3")
);
return 0;
}
Output
a: 15
divisible by 3 but not 2
a: 16
divisible by 2 but not 3
a: 17
not divisible by 2, not divisible by 3
a: 18
divisible by 2 and 3
Example 2
In this program, we have used nested if–else statements for the same purpose
instead of conditional operators −
#include <stdio.h>
int main(){
int a = 15;
printf("a: %d\n", a);
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 6/7
6/16/24, 11:52 AM Ternary Operator in C
if(a % 2 == 0){
if (a % 3 == 0){
printf("divisible by 2 and 3");
}
else {
printf("divisible by 2 but not 3");
}
}
else{
if(a % 3 == 0){
printf("divisible by 3 but not 2");
}
else {
printf("not divisible by 2, not divisible by 3");
}
}
return 0;
}
Output
When you run this code, it will produce the following output −
a: 15
divisible by 3 but not 2
https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm 7/7