ITDeveloper : C Programming
C Programming
Step by Step Tutorials
![]() Share with a Friend |
C - (?:) Ternary Statement
We use the ternary operator in C to run one code when the condition is true and another code when the condition is false.
Syntax of Ternary Operator
The syntax of ternary operator is :
testCondition ? expression1 : expression 2;
The testCondition is a boolean expression that results in either true or false. If the condition is
- true- expression1 (before the colon) is executed
- false- expression2 (after the colon) is executed
The ternary operator takes 3 operands (condition, expression1 and expression2). Hence, the name ternary operator.
For example,
(age >= 18) ? printf("Can Vote") : printf("Cannot Vote");
Here, when the age is greater than or equal to 18, Can Vote is printed. Otherwise, Cannot Vote is printed.
Program: C Ternary Operator
#include <stdio.h>
int main() {
int age;
// take input from users
printf("Enter your age: ");
scanf("%d", &age);
// ternary operator to find if a person can vote or not
(age >= 18) ? printf("You can vote") : printf("You cannot vote");
return 0;
}
OUTPUT 1:
Enter your age: 15
You cannot vote
In the above example, we have used a ternary operator that checks whether a user can vote or not based on the input value. Here,
- age >= 18- test condition that checks if input value is greater or equal to 18
- printf("You can vote")- expression1 that is executed if condition is true
- printf("You cannot vote")- expression2 that is executed if condition is false
Here, the user inputs 15, so the condition becomes false. Hence, we get You cannot vote as output.
OUTPUT :
Enter your age: 20
You can vote
This time the input value is 20 which is greater than 18. Hence, we get You can vote as output.
Assign the ternary operator to a variable
In C programming, we can also assign the expression of the ternary operator to a variable. For example,
variable = condition ? expression1 : expression2;
Here, if the test condition is true, expression1 will be assigned to the variable. Otherwise, expression2 will be assigned.
Let's see an example
#include <stdio.h>
int main() {
// create variables
char operator = '+';
int num1 = 8;
int num2 = 7;
// using variables in ternary operator
int result = (operator == '+') ? (num1 + num2) : (num1 - num2);
printf("%d", result);
return 0;
}
// Output: 15
In the above example, the test condition (operator == '+') will always be true. So, the first expression before the colon i.e the summation of two integers num1 and num2 is assigned to the result variable.
And, finally the result variable is printed as an output giving out the summation of 8 and 7. i.e 15.
Ternary Operator Vs. if...else Statement in C
In some of the cases, we can replace the if...else statement with a ternary operator. This will make our code cleaner and shorter.
Let's see an example:
#include <stdio.h>
int main() {
int number;
printf ("Enter Number : ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("Even Number");
}
else {
printf("Odd Number");
}
return 0;
}
OUTPUT :
Enter Number : 5
Odd Number
We can replace this code with the following code using the ternary operator.
#include <stdio.h>
int main() {
int number;
printf ("Enter Number : ");
scanf("%d", &number);
(number % 2 == 0) ? printf("Even Number") : printf("Odd Number");
return 0;
}
OUTPUT 1:
Enter Number : 12
Even Number
OUTPUT 2:
Enter Number : 15
Odd Number
Program : Write a program to input 2 numbers and print the highest
#include <stdio.h>
int main() {
int a, b, high;
printf ("Enter Number 1 : ");
scanf("%d", &a);
printf ("Enter Number 2 : ");
scanf("%d", &b);
high = (a > b) ? a : b;
printf ("Highest : %d", high);
return 0;
}
OUTPUT:
Enter Number 1 : 9
Enter Number 2 : 3
Highest : 9
Nested ternary operator
Program : Write a program to input 3 numbers and print the highest
#include <stdio.h>
int main() {
int a, b, c, high;
printf ("Enter Number 1 : ");
scanf("%d", &a);
printf ("Enter Number 2 : ");
scanf("%d", &b);
printf ("Enter Number 3 : ");
scanf("%d", &c);
high = (a > b) ? ((a > c) ? a : c ) : ((b > c) ? b : c );
printf ("Highest : %d", high);
return 0;
}
OUTPUT:
Enter Number 1 : 5
Enter Number 2 : 7
Enter Number 3 : 3
Highest : 7