Conditional operators:
Conditional operators return one value if condition is true and returns another value is condition is false.
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
What is a Conditional Operator in C ?
The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.
The operands may be an expression, constants, or variables. It starts with a condition, hence it is called a conditional operator. Conditional operators return one value if the condition is true and returns another value if the condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
OR
The conditional operator is of the form
variable = Expression1 ? Expression2 : Expression3
Example : (x > 20 ? 0 : 1);
Write a C program to find the maximum in the given two numbers using the conditional operator.
#include<stdio.h>
int main()
{
float num1, num2, max;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum of %.2f and %.2f = %.2f",
num1, num2, max);
return 0;
}