Ternary Operator in C: A Compact Conditional Tool
The ternary operator (?:
) in C serves as a conditional operator, allowing you to simplify your code by condensing multiple if-else statements into a more concise form. This section explains the syntax and usage of the ternary operator for efficient coding.
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. It's often used to condense multiple conditional (if-else) statements into a more compact form.
Syntax of Ternary Operator in C
The syntax for the ternary operator is:
exp1 ? exp2 : exp3
It uses three operands:
exp1
- A Boolean expression that evaluates to true or false.exp2
- Returned by the operator whenexp1
is true.exp3
- Returned by the operator whenexp1
is false.
Example 1: Ternary Operator in C
This C program uses the ternary operator to check if the value of a variable is even or odd:
Code
#include <stdio.h>
int main() {
int a = 10;
(a % 2 == 0) ? printf("%d is Even \n", a) : printf("%d is Odd \n", a);
return 0;
}
Output
10 is Even
Change the value of a
to 15 and run the code again. The output will be:
15 is Odd
Example 2
This example rewrites the logic of checking odd/even numbers using the traditional if-else
construct:
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
This program compares two variables, a
and b
, and assigns the greater value to c
using the ternary operator:
Code
#include <stdio.h>
int main() {
int a = 100, b = 20, c;
c = (a >= b) ? a : b;
printf("a: %d b: %d c: %d\n", a, b, c);
return 0;
}
Output
a: 100 b: 20 c: 100
Example 4
The equivalent program using if-else
statements for the same purpose:
Code
#include <stdio.h>
int main() {
int a = 100, b = 20, c;
if (a >= b) {
c = a;
} else {
c = b;
}
printf("a: %d b: %d c: %d\n", a, b, c);
return 0;
}
Output
a: 100 b: 20 c: 100
Example 5
When you need to include multiple statements in the true and/or false operand of the ternary operator, separate them with commas:
Code
#include <stdio.h>
int main() {
int a = 100, b = 20, c;
c = (a >= b) ? (printf("a is larger "), c = a) : (printf("b is larger "), c = b);
printf("a: %d b: %d c: %d\n", a, b, c);
return 0;
}
Output
a is larger a: 100 b: 20 c: 20
Example 6
The equivalent program using if-else
statements:
Code
#include <stdio.h>
int main() {
int a = 100, b = 20, c;
if(a >= b) {
printf("a is larger \n");
c = a;
} else {
printf("b is larger \n");
c = b;
}
printf("a: %d b: %d c: %d\n", a, b, c);
return 0;
}
Output
a is larger
a: 100 b: 20 c: 100
Nested Ternary Operator
The ternary operator can be nested just like if-else
statements. The syntax is:
exp1 ? (exp2 ? expr3 : expr4) : (exp5 ? expr6 : expr7)
C first checks if exp1
is true. If so, it evaluates exp2
. If exp2
is true, the result is expr3
; otherwise, it is expr4
. If exp1
is false, it evaluates exp5
and returns either expr6
or expr7
based on its result.
Example 1
This program determines whether a number is divisible by 2 and 3, by 2 but not 3, by 3 but not 2, or neither:
Code
#include <stdio.h>
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
Try different values:
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
Here’s the same logic using if-else
statements:
Code
#include <stdio.h>
int main() {
int a = 15;
printf("a: %d\n", a);
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
a: 15
divisible by 3 but not 2