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

## 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 [â€¦]