C Ternary Operator
In C, the ternary operator ?: is a conditional operator that provides a shorthand way to write an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false.
The ternary operator is useful for writing concise and efficient conditional expressions.
In this tutorial, we will learn its syntax, usage and nesting of ternary operators with examples.
Flow Diagram of C Ternary Operator
Following is the flow diagram of Ternary Operator in C.
Syntax of C Ternary Operator
The syntax to use the ternary operator is:
condition ? expression1 : expression2;
Explanation:
condition: The expression that is evaluated. If true,expression1executes; otherwise,expression2executes.?: Separates the condition from the true expression.expression1: Executes if the condition is true.:: Separates the true expression from the false expression.expression2: Executes if the condition is false.
Ternary Operator can be interpreted using if-else statement as below.
if (condition) {
x = expression1;
} else {
x = expression2;
}
Examples of the Ternary Operator
1. Checking if a Number is Even or Odd
In this example, we will use the ternary operator to determine whether a number is even or odd.
main.c
#include <stdio.h>
int main() {
int num = 7;
// Using ternary operator to check even or odd
(num % 2 == 0) ? printf("Even\n") : printf("Odd\n");
return 0;
}
Explanation:
- We declare an integer variable
numand assign it the value7. - The condition
num % 2 == 0checks whethernumis divisible by 2. - If true,
printf("Even\n")executes. - If false,
printf("Odd\n")executes.
Output:
Odd
2. Finding the Maximum of Two Numbers
In this example, we use the ternary operator to find the maximum of two numbers.
main.c
#include <stdio.h>
int main() {
int a = 10, b = 20;
// Using ternary operator to find the maximum number
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
return 0;
}
Explanation:
- We declare two integer variables
aandb, initialized to 10 and 20. - The condition
a > bchecks ifais greater thanb. - If true,
maxis assigneda. - If false,
maxis assignedb. - The maximum value is printed using
printf().
Output:
Maximum: 20
3. Checking if a Number is Positive or Negative
In this example, we use the ternary operator to check whether a number is positive or negative.
main.c
#include <stdio.h>
int main() {
int num = -5;
// Using ternary operator to check positive or negative
(num >= 0) ? printf("Positive\n") : printf("Negative\n");
return 0;
}
Explanation:
- We declare an integer variable
numand assign it the value-5. - The condition
num >= 0checks if the number is positive or zero. - If true,
printf("Positive\n")executes. - If false,
printf("Negative\n")executes.
Output:
Negative
Conclusion
In this tutorial, we covered the ternary operator ?: in C. The important points to remember are:
- The ternary operator provides a concise way to write conditional expressions.
- It evaluates a condition and returns one of two values based on the result.
- It is useful for simple decision-making in C programs.
