# C Language: Conditional Operator (? :) Tutorial | Ternary Operator/ Conditional operator tutorial with Examples

### Conditional Operator ( ? and : )

** Conditional operator is a ternary operator.** Ternary operator means it requires three operands.

i.e Left side expression, Middle Expression, Right side Expression.

This is written as.

**Value = Expression 1 ? Expression 2 : Expression 3 ;**

### How Conditional Operator Works :

**if Expression 1 is true or non zero then the return value is Middle Expression that is Expression 2. If the Expression 1 is false or Zero then return value is Expression 2 i.e Right side Expression.**

__Example :__**Max = X < Y ? Y : X ;**

**Max = 10 < 20 ? 20 : 10 ;**

**a = 5 > 8 : 20 : 30 ;**

### Quick Notes about Conditional Operator :

**Number of Question marks(?) and Colons(:) should be Equal.**

**Every Colon should be Followed by Question mark.**

### Program : Understanding Conditional operator with Example Program :

__Program to calculate Minimum Value among three Numbers :__-
**#include <stdio.h>** -
**int main(void) {** -
**int x,y,z,min;** -
**x = 10;** -
**y = 20;** -
**z = 5 ;** -
**min = (x<y && x<z) ? (x) : (y<z) ? (y) : (z) ;** -
**printf(“Minimum Value is : %d n“,min);** -
**return 0;** -
**}**

__Output :__Minimum Value is : 5

#### Related 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.**

## 1 Response

[…] Conditional Operator. […]