Conditional Operators in C

Conditional Operator

Conditional Operator is a ternary Operator,therefore its requires three operands.The conditional Operator look like Expression ( ? : .).

Conditional operators are used to evaluate a condition that’s applied to one or two  Boolean expressions. The result of the evaluation is either true or false.

‘?’ This operator condition to check condtion either true of false if the condition is true then returns the result of Expression2 otherwise if the condition ‘:’ is false then we will execute and return the result of ‘.’ We may replace the use of if..else statements by conditional operators.

Conditional_operator

Implementation  and properties of Condition Operator

  • It always evaluates one of the two expressions specified after the conditional operator symbol. It never evaluates both.
  • The first operand must be of integral or pointer type.
  • The second and third operands must be convertible to one another and not mutually convertible to some other type.
  • The type of result is the common type, which is an l-value, but only if both second and third operands are of the same type and both are l-values.

 

Code for Conditional Operator in C

#include<stdio.h>

int main()
{
int var=75;
int var2=56;
int num;

num= sizeof var ? (var >23 ? ((var==75)? 'A':0):0):0;
printf("%d",num);
}

Output

65

The result is 65 … because all three conditions are true…sizeof(var)
Shows the size of int (which is other than 0 ….and then after all condition are executed it will return A and the decimal of A is 65 ..
according to ASCII TABLE