DEV Community

Cover image for TERNARY OPERATORS IN C LANGUAGE
Swapnil7000
Swapnil7000

Posted on • Edited on

TERNARY OPERATORS IN C LANGUAGE

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

USING TERNARY OPERATORS:-
Alt Text

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

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 ac 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❤❤.

Top comments (5)

Collapse
 
heydeepak profile image
Deepak Choudhary

Good job...

Collapse
 
swapnil7000 profile image
Swapnil7000

Thank you

Collapse
 
kimatadebonair profile image
Alvin Kimata. 🇰🇪 • Edited

I've not only enjoyed reading your article, but I have easily understood it. Which IDE are you using in writing the above codes? Which Operating System does the IDE run on?

Collapse
 
swapnil7000 profile image
Swapnil7000

Thanks a lot for appreciating but this is not an ide i generated that image by carbon.now.sh/ this webpage check this out it is cool for generating images like this

Collapse
 
curiousgeekayon profile image
curiousGeekAyon

how it's left to right ..it seems like going left to right