Conditional or Ternary Operator
Conditional or Ternary Operator
:) in C
The conditional operator in C is kind of similar to the if-else statement as it
follows the same algorithm as of if-else statement but the conditional operator
takes less space and helps to write the if-else statements in the shortest way
possible. It is also known as the ternary operator in C as it operates on three
operands.
Syntax of Conditional/Ternary Operator in C
The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;
Or the syntax can also be in this form
variable = (condition) ? Expression2 : Expression3;
Or syntax can also be in this form
(condition) ? (variable = Expression2) : (variable = Expression3);
Conditional/Ternary Operator in C
#include <stdio.h>
int main()
{
int m = 5, n = 4;
return 0;
}
Output
m is greater than n that is 5 > 4