large= a> b? a : b;
This example illustrates how a conditional expression can be used to assign one of two alternative values to a variable depending on the outcome of some condition. As the conditional expression operator has higher precedence than the assignment operator, the above assignment statement is equivalent to
large= (a> b? a : b);
The value of the conditional expression (a > b ? a : b) is equal to that of variable a if a is greater than b; otherwise, it is equal to the value of variable b. This value is assigned to variable large. Thus, the given statement assigns the larger of variables a and b to variable large. The same effect can be achieved using an if-else statement given below.
if (a > b)
large = a;
else large = b;
We can rewrite the given conditional expression using the assignment expressions as alternative expressions, as shown below. Note that the parentheses are essential as the assignment operator has lower precedence than the conditional expression operator.
a> b? (large= a) : (large= b);
We can also use a conditional expression as an argument in the printf statement, as shown below:
printf(“Larger number: %d”, a> b? a : b);
We can also rewrite the above statement by calling the printf functions from within the conditional expression, as shown below.
a> b? printf(“Larger number: %d”, a) : printf(“Larger number: %d”, b);
Recall that the printf (and scanf)function actually returns an integer value that is ignored in most calls. Thus, the printf function calls in the above statement are expressions as expected and not statements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,large;
printf(“Enter the Value of a : “);
scanf(“%d”, &a);
printf(“\nEnter the Value of b : “);
scanf(“%d”, &b);
large= a> b? a : b;
printf(“The Large value is = %d”, large);
getch();
}