Conditional/Ternary Operators in C

The conditional or ternary operator is used to perform the conditional operation on three operands or variables. e.g. If the condition is true, it returns first value i.e. TRUE case value, otherwise returns second value i.e. FALSE case value.

Here is a C program to demonstrate the conditional operator

/**
* To demonstrate the conditional ( Ternary ) operator
*/
#include<stdio.h>
int main ( int argc, char **argv )
{
int x = 10;
int y = 12;
int res = 0;
/* if x is greater than y, it will return x,
* otherwise returns y
*/
res = ( x > y ) ? x : y;
printf (" Result = %d\n", res );
/* If x is less than y, it will return x,
* otherwise returns y.
*/
res = ( x < y ) ? x : y;
printf (" Result = %d\n", res );
return 0;
}

Hope, it would be useful !!

To contribute :

If you like Advance Computing and would like to contribute, you can  mail your article to “computingadvance@gmail.com”. You will get credit as your name , email id, designation with article on this blog.

Leave a Reply