C program: What is a ternary operator? When should it be used and when should it be avoided?
Ternar operators is a operator in which we takes three arguments where first is condition, second is expression which is true and third is expression is false. This is replacement of if else statement.
Syntax:
condition ? expression true : expression false
Ternary operator is used when we want to reduce the lines of code by simply taking three arguments.
Example:
The required code is in C language:
#include <stdio.h>
int main()
{
int a = 8, b = 10, c;
c = (a < b) ? a : b;
printf("%d", c);
return 0;
}
Ternary operator should be avoided when there is a more than 2 condtions.
Get Answers For Free
Most questions answered within 1 hours.