This operator also known as conditional operator . It is similar to if-else statement.
Syntax of ? operator
1 | Expression1 ? Expression2 : Expression 3; |
There are three expressions in ? operator, Expression1, Expression2, and Expression3 .
The value of a ? expression is determined like this −
- If Exp1 is true, then Exp2 is evaluated.
- If Exp1 is false, then Exp3 is evaluated.
Example: write a program to find the grater between two number using ? operator.
1 2 3 4 5 6 7 8 9 | #include<stdio.h> int main() { int a,b,c; a=10; b=20; c = (a>b) ? a : b ; printf("\ngrater value = %d", c); } |
OUTPUT
1 | grater value = 20 |