Conditional Operators in C

Conditional Operator

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

Conditional Operators in C

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.

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

Run
#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);

return 0;
}

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 .

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription