TERNARY OPERATORS IN C LANGUAGE
Tags: c, beginners
Published: 2019-09-26 04:15:57

HOW TO WRITE A C PROGRAM TO COMPARE THREE NUMBER WITHOUT USING IF-ELSE STATEMENTS?

**Remarks:-**
The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:

● The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.

● If the first operand evaluates to true (1), the second operand is evaluated.

● If the first operand evaluates to false (0), the third operand is evaluated.

The result of the conditional operator is the result of whichever operand is evaluated — the second or the third. Only one of the last two operands is evaluated in a conditional expression.

Conditional expressions have right-to-left associativity. The first operand must be of integral or pointer type. The following rules apply to the second and third operands:

● If both operands are of the same type, the result is of that type.

● If both operands are of arithmetic or enumeration types, the usual arithmetic conversions (covered in Standard Conversions) are performed to convert them to a common type.

● If both operands are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.

● If both operands are of reference types, reference conversions are performed to convert them to a common type.

● If both operands are of type void, the common type is type void.

● If both operands are of the same user-defined type, the common type is that type.

● If the operands have different types and at least one of the operands has user-defined type then the language rules are used to determine the common type. (See warning below.)

Any combinations of second and third operands not in the preceding list are illegal. The type of the result is the common type, and it is an l-value if both the second and third operands are of the same type and both are l-values.

**USING NESTED IF ELSE STATEMENT:-**
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rqms6lu55x63vvad9vep.png)

**USING TERNARY OPERATORS:-**
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/mc1os1s5ynwcnw247h01.png)

int main()
{ 
int a,b,c,max;//Declaration of variables

   printf("Enter three numbers to find maximum");/*Asking the users to enter three numbers*/

scanf("%d%d%d",&a,&b,&c);/*To scan the value of the numbers*/

max=a>b?a >c?a:c:b>c?b:c;/*Terniary operator to find maximum of three numbers*/

printf("%d",max);/*To print the maximum value */
}
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/quwbk939forvpvrdlofe.png)

**EXPLANATION TO THE GIVEN CODE:-**


In this program firstly we declare four variables three numbers to compare and one max variable in which maximum value is stored and then we asked user to enter three numbers then we use terniary operators to compare three numbers and then find max of three numbers and store it to max variable firstly it will check that is a>b if a>b then it will check that is a>c if this is true it will copy value of  a in max and will print a.If a<c then c will be assigned to max and on printing max c will be printed.Else it will check that b>c or not if it is true then b will be assigned to max and b will gets printed if this is not true then c will be assigned to max and will be printed.

**Syntax of ternary operators:-**

CONDITION?TRUE STATEMENT:FALSE STATEMENT

**RESULT:-**
At the end i want to conclude that You can make the above program using nested if-else statement as well as using terniary operators.I have preffered terniary operators over nested if else as it has less lines of code which makes your code more readable.That is the main reason for writing the blog.I hope you enjoyed it❤❤.