Header Ads Widget

What is Conditional Operator in C

 Conditional operator 

(1) Conditional operator  is also called ternary operator that work on 3 operands.

(2)  Represented by symbol ? : 

(3) Usually can be used as conditional statement like if-else .

Syntax : -
Cond_exp1 ? exp2 : exp 3 ;

Here cond_exp1  specifies  some condition  if this condition evaluated  to true  then exp 2 is executed  otherwise  exp 3 with executed.

example ;-

#include<stdio.h>
int main()
{
int n1 , n2, x ;
n1 = 10;
n2 = 5 ;
x = n1 > n2 ? 1 : 0 ;
// since 10>5 is true so exp2 will
// be executed that is
// 1 will be printed
printf("%d",x); // x=1
return 0;
}

Example 2:-

#include<stdio.h>
int main()
{
int n1 , n2, x ;
n1 = 10;
n2 = 50 ;
x = n1 > n2 ? 1 : 0 ;
// since 10>50 is false so exp3 will
// be executed that is
// 0 will be printed
printf("%d",x); // x=1
return 0;
}



<<Pre--Type Conversion
👈

👉File Inclusion--Next>>

Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-