# Conditional Operator in C Language (? :) with Examples

## 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:

1 |
Condition ? TrueExpression : FalseExpression |

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 :

1 |
Max = X < Y ? Y : X ; |

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

1 |
Max = 10 < 20 ? 20 : 10 ; |

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

1 |
a = 5 > 8 : 20 : 30 ; |

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:

1 |
a = 10 ? 20 : 30 ; |

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:

1 |
a = 2 > 5 ?10 : 20 : 30 ; |

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:

1 |
a = 10 < 15 ? 1 : 20 : 5 ? 30 ; |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main(void) { int x, y, z, min; // Assign numbers to three variables. // Change these values and play around x = 10; y = 20; z = 5; // We are using the conditional operator to calcualte the smallest of three numbers // We are using Nested conditional operator here. min = (x<y && x<z) ? (x) : (y<z) ? (y) : (z) ; // Print the minimum value to console printf("Minimum Value is : %d \n", min); return 0; } |

## 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:

1 |
Minimum Value is : 5 |

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:

**Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes**

## Programs Index:

## Related C Operators Tutorials :

**Arithmetic Operators with Examples.****Arithmetic operators priority and it’s Associativity.****Modulus Operator and Hidden Concepts of Modulus Operator.****Precedence Table or Operators Priority Table.****Assignment Operator, Usage, Examples****Increment Operator and Different types of Increment operators Usage with Examples.****Decrement Operator and Different types of Decrement operators with Examples.****Logical or Boolean Operators.**

## 6 Responses

[…] Conditional Operator. […]

[…] Conditional Operator. […]

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

[…] 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 […]

[…] The above program also technically does that, Let’s do it explicitly using by calculating the minimum of given two numbers using the Conditional Operator […]

[…] Conditional Operator in C […]