Conditional Operator in C

Conditional Operator in C
Techiio-author
Written by Shuvhojit DebFebruary 7, 2022
9 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this article, you'll learn what a conditional operator is.

What is a Conditional Operator in C?

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements that depend upon the output of the expression. As a conditional operator works on three operands, it is also known as the ternary operator.

The operands may be an expression, constants, or variables. It starts with a condition, hence it is called a conditional operator. Conditional operators return one value if the condition is true and returns another value if the condition is false.

Syntax

expression1? expression2: expression3;

or for simplicity, we write it as

condition ? true-statement : false-statement;

The expression1 is evaluated, it is treated as a logical condition. If the result is non-zero then expression2 will be evaluated otherwise expression3 will be evaluated. The value after evaluation of expression2 or expression3 is the final result.

The conditional operator in C works similarly to the conditional control statement if-else. Hence every code written using a conditional operator can also be written using if-else. When compared with if-else, conditional operator performance is high.

 if(expression1)
 {
     expression2;
 }
 else
 {
     expression3;
 }

Examples to Implement Conditional Operator in C

We will use these operators to understand the working of conditional operators in C with implementation.

Example #1

Code:

#include <stdio.h>
int main()
{
int p = 20 , q = 20 , r = 30 , outcome ;
outcome = ( p == q ) && ( r > q ) ;
printf ( " The result of ( p == q ) && ( r > q ) is %d \n " , outcome ) ;
outcome = ( p == q ) && ( r < q ) ;
printf ( " The result of ( p == q ) && ( r < q ) is %d \n " , outcome ) ;
outcome = ( p == q ) || ( r < q ) ;
printf ( " The result of  ( p == q ) || ( r < q ) is %d \n " , outcome ) ;
outcome = ( p != q ) || ( r < q ) ;
printf ( " The result of  ( p != q ) || ( r < q ) is %d \n " , outcome ) ;
outcome = ! ( p != q ) ;
printf ( " The result of  ! ( p == q ) is %d \n " , outcome ) ;
outcome = ! ( p == q ) ;
printf ( " The result of  ! ( p == q ) is %d \n " , outcome ) ;
return 0 ;
}

Output:

blogpost

In the above code, we have declared 4 integers p, q, r, and outcome in which the outcome will store the outcome of all the applied conditions on the remaining 3 integer variables. Then one by one we applied these &&, ||, and! = operations on the declared variables. So the values will be compared as per the mentioned code and results will be stored in outcome integer.

Example #2

Code:

#include <stdio.h>
int main()
{
int marks_obtained ;
printf ( " Please enter the obtained marks : " ) ;
scanf ( " %d " , &marks_obtained ) ;
if ( marks_obtained >= 36 )
printf ( " \n Congratulations!! You Passed " ) ;
else
printf ( " \n You Failed " ) ;
return 0 ;
}

Input:

blogpost

Output:

blogpost

Example #3

Code:

#include <stdio.h>
int main()
{
int marks_obtained ;
printf ( " Please enter the marks obtained : " ) ;
scanf ( " %d " , &marks_obtained ) ;
puts ( marks_obtained >= 36 ? " Congratulations!! You Passed" : " You Failed " ) ;
return 0 ;
}

Input:

blogpost

Output:

blogpost

If you compare the last two codes they are performing the same operations with the same integers and conditions. The only difference we can see in the last second is using the if-else condition to check the condition of the mark and then displaying the output whereas, in the last code, we are using conditional operators to perform the same operation with the same integers and obtained marks value. Therefore, to display why conditional operators are used, you can see from the last two codes that using a conditional operator instead of using if-else can save a few lines of code. Apart from this, it is more efficient as if one condition is already true then the compiler won’t even go to the rest of the conditions which will save compilation and execution time both. In terms of memory, a conditional operator will also save space.

Conclusion

Conditional operators in C play a crucial role as it saves a lot of time. Instead of putting an if-else loop, we can use these operators because we only have to write two conditions in one line which saves time and space both by making efficient code.

C Language
Conditional Operator
Programming
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance