Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Ternary Operator in C

In this article, you will be introduced to the ternary operator in C. The article contains some basic examples which will help you in developing an understanding of how the ternary operator works, the flow control of the ternary operator, and how to implement the ternary operator in C. In this article, you will also learn about some pros and cons of using the ternary operator in C.

What is a Ternary Operator in C

The ternary operator in c is a way to shorten the if-else block. So before moving further in the article we recommend you first go through if-else statements if you are a beginner in programming.

The ternary operator in c consists of three arguments:-

  1. The first argument in the ternary operator in c is a condition.
  2. The second argument in the ternary operator in c is an expression that is executed when a condition is true.
  3. The last argument in the ternary operator in c is also an expression that is executed when the condition is false.

So, we can say the ternary operator in c allows us to execute a different piece of code depending on the first argument,i.e on condition.

Syntax of Ternary Operator in C

Syntax: (condition)? expression1 : expression2;

Working of Syntax:

  • If the condition in the ternary operator is true, then the expression1 executes.
  • If the condition is false, then the expression2 executes.

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

int minNumber = 25 < 15 ? 25: 15;

So, if the condition 25 < 15 is true then the variable minNumber is initialized with 25 otherwise 15. As the condition is false our variable minNumber is initialized with 15. This is how the ternary operator works.

Point to remember: Ternary operators in c, like if-else, can also be nested.

Flow Chart for the Ternary Operator in C

The flow chart for a ternary operator in c looks like this:

Let us understand this flow chart of a ternary operator in c:-

Imagine we are given a ternary statement - condition? expression1: expression2, if our condition is true then control flow to expression1.Similarly, if our condition is false then control flow to expression2. Either one of the expressions gets executed and we get our result.

Example of How Ternary Operator works in C
These examples will teach you how the ternary operator in C works:

Example-1
Find out the maximum number in the given two numbers using the ternary operator in c.
Given:- x = -25 , y = -50

Code Implementation:

#include <stdio.h>

int main(void) {
    int x = -25,y = -50;
    // implementing ternary operator
    int maxNum = x>y ? x : y;
    printf("%d",maxNum);
    return 0;
}

Output

25

In the above code, we are going to find the max number between two given numbers by using the ternary operator in c.To do the same, we applied the condition x > y which compares the given two numbers and based on the result, it’s going to print the max number.

Example- 2
Find out whether the given number is odd or not using Ternary Operator in C.
Given:- n = 23

Code Implementation:

#include <stdio.h>

int main(void) {
    // given
    int n = 23;
    // ternary operator
    (n % 2 == 1) ? printf("Number is Odd") : printf("Number is not Odd");
    return 0;
}

The output obtained from the example will be:

Number is Odd

Point to Note: Always remember that the expression1 and expression2 arguments must be of the same type. Also, they should be very simple expressions.

Summary:

  • The ternary operator is a way to shorten the if-else block of code.
  • The symbol used to represent the ternary operator in c is ? :.
  • In C, the ternary operator is used to increase compiler efficiency.
  • The ternary operator is also called the Conditional operator.

Pros of using Ternary Operators in C

  • Ternary operator reduces the complexity of code.
  • Code becomes very easy to read, alter and manage in the long run.
  • Using the ternary operator in c, it is very easy to identify and edit any present errors in the code.

Cons of using Ternary Operator in C
A ternary operator does not have any return type. The return type depends entirely on the convertibility of the expressions. In any other given case, the compiler will throw an error.

Leave a Reply

Your email address will not be published. Required fields are marked *