Ternary Operator in C Explained in Details with Examples.
Conditional or Ternary Operator (?:)
Ternary operator is used to perform any operation on three operands
Syntax :
Expression1 ? Expression2 : Expression3;
Here
Expression1 is a condition
Expression2 is a operation
Expression3 is a operation
In the above syntax
if condition is true ,The statements of Expression2 is execute.
if condition is false ,The statements of Expression3 is execute
Write a C-program to print odd or even number using ternary operator
#include <stdio.h>
int main() {
int x;
printf("Enter x value");
scanf("%d",&x);
x%2==0 ? printf("Even Number") : printf("Odd Number");
return 0;
}
Output:
Enter x value 10
Even Number
Enter x value 11
Odd Number
Write a C-program to print max number using ternary operator