C Ternary Operator (With Examples)

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The C ternary operator, often represented as exp1 ? exp2 : exp3, is a valuable tool for making conditional decisions in C programming. This compact operator evaluates a condition and performs one of two expressions based on whether the condition is true or false. With its concise syntax and flexibility, the ternary operator is especially useful for streamlining conditional assignments.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax

The syntax of the Ternary Operator in C is:

Syntax:

Working of Syntax :

  • If the condition in the ternary operator is met (true), then the exp2 executes.
  • If the condition is false, then the exp3 executes.

Example of C Ternary Operator

The following example explains the working of Ternary Operator in C.

so, if the condition 10 > 15 is true (which is false in this case) mxNumber is initialized with the value 10 otherwise with 15. As the condition is false so mxNumber will contain 15. This is how Ternary Operator in C works.

NOTE: Ternary operator in C, like if-else statements, can be nested.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Assign the Ternary Operator to a Variable:

You can assign the result of the ternary operator to a variable, as shown in the previous example with the variable max. This is a common use case for the ternary operator, where the result of the condition is stored in a variable for later use.

Example:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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

The ternary operator and the if...else statement serve similar purposes but differ in syntax and use cases.

Example:

AttributeTernaryIf-else
SyntaxConcise and compactMore verbose
Use CasesSuitable for simple conditionsSuitable for complex conditions
Return ValueReturns a valueDoesn't return a value
Error HandlingLimited in handling multiple conditionsSuitable for handling multiple conditions and cases

Conclusion

  • The C Ternary Operator is represented as exp1 ? exp2 : exp3, providing a concise way to make conditional decisions.
  • If the condition exp1 is true, exp2 executes; otherwise, exp3 executes.
  • It's useful for simple conditional assignments and can improve code readability in such cases.
  • For more complex conditions, the if...else statement may be a better choice.
  • Understanding the ternary operator enhances your ability to write concise and expressive C code for conditional operations.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more