Ternary(?) Operators


C contains a very powerful and convenient operator that replaces certain statements of the if-then-else form. The ternary operator '?' takes the general form :

ex1 ? ex2 : ex3

where ex1, ex2, and ex3 are expressions. Notice the use and placement of the colon.

The '?' operator works like this: ex1 is evaluated. If it is true, ex2 is evaluated and becomes the value of the expression. If ex1 is false, ex3 is evaluated and its value becomes the value of the expression. For example,


Example of ternary(?) operator

Program
// find the big number without using if-else statement
#include <stdio.h>

int main() {
  int a = 10, b = 12, c = 11;
  a>b&&a>c?printf("a is big \n"):b>c?printf("b is big\n"):printf("c is big\n");
  return 0;
}

example of ternary operator