0% found this document useful (0 votes)
57 views

Conditional or Ternary Operator (__) in C - GeeksforGeeks

The conditional or ternary operator in C is a concise alternative to the if-else statement, operating on three operands and allowing for shorter code. It follows the syntax 'variable = condition ? Expression2 : Expression3', executing Expression2 if the condition is true and Expression3 if false. While useful for simple conditions, excessive use can lead to complex and unreadable code.

Uploaded by

Anirban Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Conditional or Ternary Operator (__) in C - GeeksforGeeks

The conditional or ternary operator in C is a concise alternative to the if-else statement, operating on three operands and allowing for shorter code. It follows the syntax 'variable = condition ? Expression2 : Expression3', executing Expression2 if the condition is true and Expression3 if false. While useful for simple conditions, excessive use can lead to complex and unreadable code.

Uploaded by

Anirban Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

12/3/23, 4:48 PM Conditional or Ternary Operator (?

:) in C - GeeksforGeeks

Conditional or Ternary Operator (?:) in C

The conditional operator in C is kind of similar to the if-else statement as it


follows the same algorithm as of if-else statement but the conditional
operator takes less space and helps to write the if-else statements in the
shortest way possible. It is also known as the ternary operator in C as it
operates on three operands.

Syntax of Conditional/Ternary Operator in C


The conditional operator can be in the form

variable = Expression1 ? Expression2 : Expression3;

Or the syntax can also be in this form

variable = (condition) ? Expression2 : Expression3;

Or syntax can also be in this form

💡 Spotlight

Top Data Removal Services


In the digital world, where everything is accessible
in exchange for your personal information,...Read
More

Articles Spotlight Listicles

(condition) ? (variable = Expression2) : (variable = Expression3);

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 1/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

Conditional/Ternary Operator in C

It can be visualized into an if-else statement as:

if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}

Since the Conditional Operator ‘?:’ takes three operands to work, hence they
are also called ternary operators.

Note: The ternary operator have third most lowest precedence, so we


need to use the expressions such that we can avoid errors due to
improper operator precedence management.

Working of Conditional/Ternary Operator in C


The working of the conditional operator in C is as follows:

Step 1: Expression1 is the condition to be evaluated.


Step 2A: If the condition(Expression1) is True then Expression2 will be
executed.
Step 2B: If the condition(Expression1) is false then Expression3 will be
executed.

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 2/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

C Step C3:Data
C Basics Results
Types will be returned.
C Operators C Input and Output C Control Flow C Functions C Arrays C St

Read
Flowchart Discuss
of Courses Practice
Conditional/Ternary Operator in C
To understand the working better, we can analyze the flowchart of the
conditional operator given below.

Flowchart of conditional/ternary operator in C

Examples of C Ternary Operator

Example 1: C Program to Store the greatest of the two Numbers using


the ternary operator

// C program to find largest among two


// numbers using ternary operator

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 3/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

#include <stdio.h>

int main()
{
int m = 5, n = 4;

(m > n) ? printf("m is greater than n that is %d > %d",


m, n)
: printf("n is greater than m that is %d > %d",
n, m);

return 0;
}

Output

m is greater than n that is 5 > 4

Example 2: C Program to check whether a year is a leap year using


ternary operator

// C program to check whether a year is leap year or not


// using ternary operator

#include <stdio.h>

int main()
{
int yr = 1900;

(yr%4==0) ? (yr%100!=0? printf("The year %d is a leap year",yr)


: (yr%400==0 ? printf("The year %d is a leap year",yr)
: printf("The year %d is not a leap year",yr)))
: printf("The year %d is not a leap year",yr);
return 0;
}

//This code is contributed by Susobhan AKhuli

Output

The year 1900 is not a leap year

Conclusion

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 4/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

The conditional operator or ternary operator in C is generally used when we


need a short conditional code such as assigning value to a variable based on
the condition. It can be used in bigger conditions but it will make the
program very complex and unreadable.

FAQs on Conditional/Ternary Operators in C

1. What is the ternary operator in C?

The ternary operator in C is a conditional operator that works on three


operands. It works similarly to the if-else statement and executes the code
based on the specified condition. It is also called conditional Operator

2. What is the advantage of the conditional operator?

It reduces the line of code when the condition and statements are small.

Whether you're preparing for your first job interview or aiming to upskill in
this ever-evolving tech landscape, GeeksforGeeks Courses are your key to
success. We provide top-quality content at affordable prices, all geared
towards accelerating your growth in a time-bound manner. Join the millions
we've already empowered, and we're here to do the same for you. Don't
miss out - check it out now!

Last Updated : 03 Apr, 2023 93

Previous Next

Increment and Decrement Operators in sizeof operator in C


C

Similar Reads
Implementing ternary operator without C/C++ Ternary Operator - Some
any conditional statement Interesting Observations

Program to Find the Largest Number C++ | Nested Ternary Operator


using Ternary Operator

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 5/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

Set a variable without using Arithmetic, Ternary Search in C


Relational or Conditional Operator

Output of C programs | Set 55 (Ternary Print "Even" or "Odd" without using


Operators) conditional statement

Operands for sizeof operator Result of comma operator as l-value in


C and C++

Article Contributed By :
khalidbitd
K khalidbitd

Vote for difficulty


Current difficulty : Easy

Easy Normal Medium Hard Expert

Improved By : sharmaparth5399, susobhanakhuli, abhishekcpp


Article Tags : C-Operators , C Language

Improve Article Report Issue

sizeof operator in C
Sizeof is a much-used operator in the C. It is a compile-time unary
operator which can be used to co...Read More

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 6/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks
A-143, 9th Floor, Sovereign Corporate
Tower, Sector-136, Noida, Uttar Pradesh -
201305

Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor

Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets

Data Science & ML HTML & CSS


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning Tutorial Bootstrap
Maths For Machine Learning Tailwind CSS
Pandas Tutorial SASS
NumPy Tutorial LESS
Deep Learning Tutorial Web Design

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 7/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

Computer Science Python


GATE CS Notes Python Programming Examples
Operating Systems Django Tutorial
Computer Network Python Projects
Database Management System Python Tkinter
Software Engineering OpenCV Python Tutorial
Digital Logic Design Python Interview Question
Engineering Maths

DevOps Competitive Programming


Git Top DS or Algo for CP
AWS Top 50 Tree
Docker Top 50 Graph
Kubernetes Top 50 Array
Azure Top 50 String
GCP Top 50 DP
DevOps Roadmap Top 15 Websites for CP

System Design JavaScript


What is System Design TypeScript
Monolithic and Distributed SD ReactJS
High Level Design or HLD NextJS
Low Level Design or LLD AngularJS
Crack System Design Round NodeJS
System Design Interview Questions Express.js
Grokking Modern System Design Lodash
Web Browser

NCERT Solutions School Subjects


NCERT Solutions for Class 12 Mathematics
NCERT Solution for Class 11 Physics
NCERT Solutions for Class 10 Chemistry
NCERT Solutions for Class 9 Biology
NCERT Solutions for Class 8 Social Science
Complete Study Material English Grammar

Commerce Management & Finance

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 8/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

Accountancy Management
Business Studies HR Managament
Indian Economics Income Tax
Macroeconomics Finance
Microeconimics Economics
Statistics for Economics

UPSC SSC/ BANKING


Polity Notes SSC CGL Syllabus
Geography Notes SBI PO Syllabus
History Notes SBI Clerk Syllabus
Science and Technology Notes IBPS PO Syllabus
Economics Notes IBPS Clerk Syllabus
Important Topics in Ethics SSC CGL Practice Papers
UPSC Previous Year Papers

Colleges Companies
Indian Colleges Admission & Campus Experiences IT Companies
Top Engineering Colleges Software Development Companies
Top BCA Colleges Artificial Intelligence(AI) Companies
Top MBA Colleges CyberSecurity Companies
Top Architecture College Service Based Companies
Choose College For Graduation Product Based Companies
PSUs for CS Engineers

Preparation Corner Exams


Company Wise Preparation JEE Mains
Preparation for SDE JEE Advanced
Experienced Interviews GATE CS
Internship Interviews NEET
Competitive Programming UGC NET
Aptitude Preparation CAT
Puzzles

More Tutorials Write & Earn


Software Testing Write an Article
Software Development Improve an Article

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 9/10
12/3/23, 4:48 PM Conditional or Ternary Operator (?:) in C - GeeksforGeeks

Product Management Pick Topics to Write


SAP Share your Experiences
SEO Internships
Linux
Excel

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/ 10/10

You might also like