Ternary Operator
Written by
Ternary operator or ?: operator
The ternary operator is also known as a conditional operator.
The conditional operator works like this :
Exp 1 ? Exp 2 : Exp 3;
- Here,
- If expression 1 is true then expression 2 is executed.
- If expression 1 is false then expression 3 is executed.
Example :
#include <stdio.h>
int main() {
int a=1, b ; b = ( x ==1 ? 2 : 0 ) ; printf(“Value of a is %d\n”, a); printf(“Value of b is %d”, b); return 0; }
Output :
Value of a is 1
Value of b is 2