The programmers utilize the ternary operator in case of decision making when longer conditional statements like if and else exist. In simpler words, when we use an operator on three variables or operands, it is known as a Ternary Operator. We can represent it using ? : . Thus, it is also known as a conditional operator.
In this article, we will discuss the ternary operator in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.
Table of Contents
- Arguments Of Ternary Operator
- Use Of If And Else Statements
- Syntax Of Ternary Operator In C
- Example Of How We Use The Ternary Operator
- Cons Of Using The Ternary Operator In C
- Practice Problems On Arithmetic Operator In C
- Faqs On Ternary Operators In C
Arguments of Ternary Operator
A ternary operator takes three major arguments:
- The comparison argument
- The result upon a false comparison
- The result upon a true comparison
Use of if and else Statements
You can think of a ternary operator as a shorthand way in which we write an if-else statement. Here is a very simple example of decision-making using the if and else statements:
int p = 10, q = 20, r;
if (p < q) {
r = p;
}
else {
r = q;
}
printf(“%d”, r);
The example mentioned above needs more than 10 lines. Such a thing won’t be necessary using a ternary operator. Using it, you can easily write the same afore-mentioned program in 3 lines of code.
Syntax of Ternary Operator in C
condition ? value_if_true : value_if_false
This very statement evaluates to a value_if_true only when the condition is met. Otherwise, it evaluates to a value_if_false.
Here is how we can write the example mentioned above using a ternary operator in C:
int p = 10, q = 20, r;
r = (p < q) ? p : q;
printf(“%d”, r);
The output obtained out of the example mentioned above will be:
10
Here, we have set r to be equal to p, because the condition of p < q was considered true.
Always remember that the value_if_false and value_if_true arguments need to be of the same type. Also, they must not be full statements. Rather, they should be very simple expressions.
Example of How we use the Ternary Operator
Just like the if-else statements, the ternary operator can be nested. Look at the code given below:
int p = 1, q = 2, ans;
if (p == 1) {
if (q == 2) {
ans = 3;
} else {
ans = 5;
}
} else {
ans = 0;
}
printf (“%d\n”, ans);
Here is how we will write the very same code using the ternary operator. The code required is comparatively very simple:
int p = 1, q = 2, ans;
ans = (p == 1 ? (q == 2 ? 3 : 5) : 0);
printf (“%d\n”, ans);
Here, the output obtained out of both the sets of the codes mentioned above (normal as well as ternary) has to be:
3
When we use a ternary code in C, it cuts the downtime of coding and reduces the line complexity. Thus, it becomes very easy for a programmer to identify and edit any errors present in the code accurately and easily. Moreover, the final code becomes very easy to read, understand, manage, and alter in the long run.
Cons of Using the Ternary Operator in C
Here are some of the very unknown facts that tag along with the ternary operator. Take a look:
Return Type – A ternary operator does not have any return type. The return type, in this case, depends entirely on the convertibility of the expression3 into expression2. In any other given case, the compiler will ultimately throw an error.
Side Effects – If expression1 has any side effect associated with it, it will be updated/ changed immediately before the execution of the expression3 or expression2. It means that if there exists a side effect, then the evaluation of only one of them would occur.
Practice Problems on Ternary Operator in C
1. Pick the correct syntax for Ternary Operator in C from the options given below:
A) condition < expression1 ? expression2
B) condition ? expression1 < expression2
C) condition : expression1 ? expression2
D) condition ? expression1 : expression2
Answer – D)
Explanation – When the given condition is true, expression1 will be evaluated, but when the condition is false, then expression2 will be evaluated.
2. Find the output of the given C statement.
int main()
{
int p=0;
p = 5<2 ? 4 : 3;
printf(“%d”,a);
return 0;
}
A) 2
B) 5
C) 3
D) 4
Answer – C)
Explanation – Here, 5 < 2 is false. Thus, 3 will be automatically picked and will be assigned to the given variable p.
FAQs on Ternary Operators in C
Why do we use the Ternary operator in C?
The ternary operator in C serves as an alternative syntax for the conditional statement of if-else. We usually utilize this statement for condensing the long and complex if-else statements.
Is a Ternary operator different from that of a conditional statement?
The ternary operator is basically a class of conditional operators. They serve the very same function. In simpler words, a typical ternary operator in C is a type of conditional operator that the programmers use for making various decisions that are nothing but the condition statements which are similar to the if and else statements.
What is the advantage of using a ternary operator over an if-else statement?
The ternary operator performs the very same function as that of the if/else condition, but it changes the overall outlook and length of the available code. The ternary operator makes a code very easy to understand, and it looks much more professional and algorithmic.
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.
Also Explore,
Comments