Conditional Operator in C Language (? :) with Examples

Spread the love
Conditional-Operator-in-C-Programming-Language

Introduction:

In our previous article, We discussed the Logical Operators in C, In today’s article, We will look at the Conditional Operator in C Langauge. Conditional Operator is the only ternary operator in the C language.

Conditional Operator in C Programming:

The Conditional Operator is a Ternary Operator, Which means it works on three operands. This operator is used for decision-making.

Conditional Operator is denoted by the : ? Syntax in C language.

Syntax of Conditional Operator:

Here the Condition is the condition to check, TrueExpression is executed when the condition is True, Otherwise, the FalseExpression will be executed.

How Conditional Operator Works :

Firstly Condition is evaluated, If Condition is True or non zero then the return value is TrueExpression and

If the Condition is False or Zero then return value is FalseExpression

Conditional Operator works exactly like the if-else statements in C Langauge. Whenever possible use the Conditional operator, It is simple and elegant.

We can also have Nested Conditional operators, i.e The conditional operator inside another conditional operator.

Let’s take a couple of examples to understand it better.

Conditional Operator Example 1 :

Here first the expression X < Y is evaluated, If the value of the expression is True then the Max is X. Otherwise, The Max value becomes X

Let’s say, The X = 10 and Y = 20 than above condition is equivalent to 

First expression 10< 20 is evaluated since it is True value 20 becomes the value of Max.

Conditional Operator Example 2:

Let’s take one more example 

Here the First condition 5 > 8 is evaluated. As it is False value of the variable a becomes 30.

Quick Notes on Conditional Operators in C Langauge:

📢 The number of Question marks(?) and Colons(:) should be Equal.

Example 1:

In the above example, The number of question marks and colons are equal. So It is a valid Conditional operator.

The result will be, The value of the variable a becomes 20 because 10 is non-zero value.

Example 2:

In the above example, The number of Question marks and Colons are not equal. So it is not a valid usage of conditional operator.

📢 Every Colon should be Followed by a Question mark.

Example 1:

The above Example generates the Error – The second colon is not followed by a Question mark.

Example Program: Calculate the Minimum of three Numbers using Conditional Operator in C:

Here we are going to use the Nested Conditional Operator to calculate the minimum value of three numbers.

Program Explanation:

In the above nested conditional operation, The first (x<y && x<z) condition is evaluated, If it is True, Then we will go to the TrueExpression. Here the TrueExpression is (x), Means we found our minimum value.

If the (x<y && x<z) condition is False, Then we will go the FalseExpression i.e (y<z) ? (y) : (z)

Here (y<z) ? (y) : (z) also a Conditional Operator, So we need to apply the same logic here as well. So the (y<z) condition is evaluated, If it is True, Then Minimum value becomes (y), Otherwise (z) becomes the minimum value.

Program Output:

As you can see, the Conditional operator is used extensively in C language to make decisions.

Conclusion:

In this article, We looked at the conditional operator in C language and different examples of conditional operator.

Tutorials Index:

Programs Index:

Related C Operators Tutorials :

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. October 31, 2017

    […] Conditional Operator. […]

  2. June 20, 2021

    […] Conditional Operator. […]

  3. July 19, 2021

    […] We have discussed the conditional operators in detail in the following post – C Language: Conditional Operator (? 🙂 Tutorial | Ternary Operator/ Conditional operator tutorial … […]

  4. July 25, 2021

    […] our previous articles, We have discussed the Logical Operators and Conditional operators in C Language. In today’s article, We are going to look at the Bitwise Operators in C programming […]

Leave a Reply