Jun 14, 2014

C Programming #11: Operators - Conditional

There is only one conditional operator in C. It is ternary operator in C. (ternary means, operator that takes 3 arguments).

Syntax is as follows



condition ? result_1 : result_2;

If the condition is true, result_1 is returned otherwise result_2 is returned. result_1 and result_2 can be expressions

Following program shows the use of conditional operator


#include <stdio.h>
int main()
{
   int a = 10;
   int b = 12;
   int c = 15;

   printf("a greater than b is %d\n", a > b ? 1 : 0);
   printf("c not equal to a is %d\n", c != a ? 100 : 200);

   return 0;
}


Output of the above program is

a greater than b is 0
c not equal to a is 100

Links

Quiz - Not Yet written !!
Next Article -  C Programming #12: Operators - Assignment
Previous Article - C Programming #10: Operators - Bit-wise
All Article - C Programming

No comments :

Post a Comment