Conditional operator statement

Conditional operator statement:

The conditional operator statement is used with the combination of ? and : and this is known as conditional operator.

 Syntax:                         conditional expression ? expression1 : expression2

The conditional expression is evaluated first and if the result is nonzero then the expression1 is returned as the value of the expression, otherwise the value of expression2 is returned.

For example:

If(a<1)

t=0;

else

t=1;

Here the above code can be written as:

t = (a<1) ? 0:1;

Program:

 

PROGRAM TO SHOW THE USE OF CONDITIONAL OPERATOR

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf(“Enter any two numbers to check which of them is greater: “);

scanf(“%d%d”,&a,&b);

c=(a>b)?a:b;          //ILLUSTRATING USE OF CONDITIONAL OPERATOR

printf(“%d is greater”,c);

getch();

}

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *