Conditional Operator Statement in C


The program demonstrates the use of the ternary operator (also known as the conditional operator). The ternary operator is a shorthand way of writing an if-else statement. The syntax for the ternary operator is as follows:

Syntax:
                condition ? value1 : value2 ;


This can be read as "if condition is true, return value1, otherwise return value2."


In the program, the ternary operator is used to determine the greater of two numbers, a and b. The condition being checked is a > b. If this condition is true, the greater of the two numbers is a, and so a is returned. If the condition is false, the greater of the two numbers is b, and so b is returned. The returned value is then stored in variable c and printed out to the console. :


Source Code

// Conditional Operator Statement ?:
#include<stdio.h>
int main()
{
    int a,b,c;
    printf("\nEnter The Value of A &  B : \n");
    scanf("%d%d",&a,&b);
    c=a>b?a:b;
    printf("\nThe Greatest No is %d",c);
    return 0;
}
 
To download raw file Click Here

Output

Enter The Value of A &  B :
23
45

The Greatest No is 45

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C