Ternary Operator in C: Ternary Operator vs. if...else Statement

Ternary Operator in C: Ternary Operator vs. if...else Statement

27 May 2024
Beginner
1.4K Views
5 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Ternary Operator in C: An Overview

In the Operators in C, we saw the definition, syntax, and one basic example of the ternary operator in C. In this tutorial, we will get into the Ternary operator in detail. Join us in our C Tutorial to examine the fundamental ideas and real-world uses of ternary operators in C programming.

ternary operator in c programming

Assign the ternary operator to a variable

We saw the syntax of a ternary operator:
testexpression? expression1 : expression2;

In the above syntax, we can assign the expression of the ternary operator to a variable like this:

testcondition = condition? expression1 : expression2;

Here, if the condition results in true, expression1 will get assigned to the variable, testcondition. If false, expression2 will get assigned to the variable.

Example


#include <stdio.h>
int main()
{
 int a = 5;
 int b = 10;
 int max = (a > b) ? a : b;
 printf("The maximum value is: %d\n", max);
 return 0;
}
In the above code in the C Online Compiler, the test-condition, “a>b” is checked. If it evaluates to true, the value of a will be printed else the value of b will be printed.

Read More - Top 50 Mostly Asked C Interview Questions

Output

The maximum value is: 10

Ternary Operator Vs. if...else Statement in C

The ternary operator may sometimes be used to replace multiple lines of code with a single line. Many a time, it is used to replace simple if-else statement. It enhances the code readability by reducing the length of your program.

Example

#include <stdio.h>
int main()
{
 int num=3;
 if (num % 2 == 0) {
 printf("Even Number");
 }
 else {
 printf("Odd Number");
 }
 return 0;
}

The above code checks whether a number is odd or even with the help of an if-else statement.

We can use the ternary operator to perform the same operation.

#include <stdio.h>
int main() {
 int num = 3;
 (num % 2 == 0) ? printf("Even Number") : printf("Odd Number");
 return 0;
}

If we compare the above two codes, we can find that the code using the ternary operator looks more readable and short as compared to the one using if-else.

If you want to use a single if-else statement to check any condition, you can go for the ternary operator. However, it is totally up to you what to select.

Summary
The above article on ternary operators covered all the aspects of it with examples. If you want to learn more and improve further, explore our C Certification program and become a certified C programmer.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification Course Jun 16 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
.NET Microservices Certification Training Jun 23 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core (Project) Jun 23 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Developer Certification Training Jun 23 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Generative AI For Software Developers Jul 14 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this