So I was just trying to write a C code for comparing numbers, which one of the three number is largest and which one is the smallest.

Then I found that I can use the ternary operator (ex: x < y ? num1 : num2), so I thought yeah okay this will work and then I wrote this code:

#include <stdio.h>
int main(){
int num1, num2, num3, largest, smallest;

printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);

//largest among three integers.

largest = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
printf("\nThe largest among the three is: %d", largest);

//smallest among three integers

smallest = num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);
printf("\nThe smallest among the three is: %d", smallest);

return 0;
}

So, if I give input like num1 = 10, num2 = 20 and num3 = 30, then the output is

Enter three integers: 10 20 30

The largest among the three is: 30
The smallest among the three is: 10

But what happens when all these numbers are equal? Will the condition of the Ternary operator be true or false?

Enter three integers: 20 20 20

The largest among the three is: 20
The smallest among the three is: 20

I don't know what the condition but the program works fine nothing wrong the output is correct.

I am using this on windows 10 latest and gcc.exe (MinGW.org GCC-6.3.0-1) 6.3.0

I am sorry if this is a lame question but I didn't found anything about this.

3

There are 3 best solutions below

0
0___________ On BEST ANSWER

> larger operator is true only if the left value is larger than the rigth value. Otherwise it will be false

Will the condition of the Ternary operator be true or false?

n = x > y ? x : y;

n will be assigned with the value of x if x is larger than y, otherwise (including the case when x == y) it will be assigned with the value of y

0
Vlad from Moscow On

If the first expression of the conditional operator evaluates to false then the result of the conditional operator is the value of the third expression.

For example in this statement

largest = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);

if num1 is not greater than num2 (that is the expression with the relational operator num1 > num2 evaluates to false) then this (third) expression evaluates

(num2 > num3 ? num2 : num3)

Again if num2 is not greater than num3 then the result of this expression and of the whole enclosing expression is the value of num3,

0
Lundin On

Generally, boolean algebra goes: the opposite of a > b ("a greater than b") is a <= b ("a less or equal to b").

If you want to cover larger than, smaller than and equal, then you'd need something like this pseudo code:

(a < b) ? smaller : ( (a > b) ? larger : equal )

Which is just an obscure and unreadable way of the equivalent preferred code:

if(a < b)
{}
else if(a > b)
{}
else // equal
{}

Always use if-else if chains over ?: chains unless you have specialized requirements, such as when writing function-like macros.