Conditional or Ternary Operator (?:) In C

Conditional or Ternary Operator (?:) In C

The conditional operator, often referred to as the ternary operator due to its use of three operands, is a fundamental feature in the C programming language. This operator provides a concise and efficient way to make decisions based on conditional expressions, allowing programmers to write compact and readable code. By utilizing the ? : syntax, it enables the selection of one of two values or expressions based on a specified condition. This versatility makes the conditional operator a valuable tool for streamlining conditional statements and enhancing code readability in C programs. In this article, we’ll explore the syntax, usage, and benefits of the conditional operator, demonstrating how it simplifies decision-making processes within C code.

Syntax of Conditional/Ternary Operator in C

variable = Expression1 ? Expression2 : Expression3;

Or Syntax can also written as

variable = (condition) ? Expression2 : Expression3;

Another way to write syntax

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

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

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

Ternary operators can even take three operands to work. Hence, it is called Ternary operator.

Working as Conditional/Ternary Operator in C

  1. It checks a condition (Expression1).
  2. If the condition is true, it executes Expression2.
  3. If the condition is false, it executes Expression3.
  4. Finally, it returns the result of either Expression2 or Expression3, depending on whether the condition was true or false.

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
  
#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;
}

Output

The year 1900 is not a leap year

FAQ- Conditional or Ternary Operator (?:) in C

Q1. What is ternary operators or conditional operators in C?

Ans. The ternary operator in C, also called the Conditional Operator, works with three values. It’s used to make conditional code shorter, compared to using if-else statements.

Q2. What is the difference between a conditional statement and a ternary operator?

Ans. Ternary operators are faster and more concise than if-else blocks. They’re single statements and can be used where if-else isn’t suitable, offering improved readability for simple conditionals.

Q3. Why is it called a ternary operator?

Ans. “Ternary” refers to things composed of three items. The ternary operator aligns with this definition, comprising a conditional, a value for true, and a value for false.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment