C - Conditional Operators

<<Previous

Next >>





Ternary Operator ?:

This operator contains a conditional statement followed by two expressions that return a value. It first evaluates the condition. If the condition returne true, then the first expression will be evaluated and value returned. Otherwise, the second expression will be evaluated and value returned. The conditional operator is also known as ternary operator which takes three arguments.

Syntax:
	condition ? (expression 1) :(expression 2);

For example:
	X=((2==3) ? 4 : 5);

The above condition (2==3) is false, so the second value 5 is assigned to X.

Following is an example C Program using Assignment Operators.

#include <stdio.h>
#include <conio.h>

int main()
{
   int A = 20;
   int B = 15;
   int C;

   C =((A>B)? 20 : 15);

   printf("The C value is : %d \n",C);

   C =((A<B)? 20 : 15);

   printf("The C value is : %d \n",C);
    return(0);
 }

Output:

The value of C is : 20
The value of C is : 15

<< Previous

Next >>