Search This Blog

Monday, May 2, 2011

Conditional operator in C

In C Programing there is only one ternary operator called conditional opertor

if there are more than two operends in a operator then it is called Ternary operator

syntx of ternary operator in C

var.=(exp?stat1. : stat2);

if expression is true then stat1 will be executed otherwise stat2 will be executed

example of conditional operator:-

int a,b,c;
a=1;
b=2;
c=a>b?a:b;        /*conditional operator*/

Now firstly it will check whether a is big or b is big. if condition true then 'a' will be assigned in 'c' variable otherwise 'b' will be assigned in 'c' variable

And 1 is not greater than 2 . its mean condition becomes false and so 'b' will be assigned in 'c' variable.


A programe to findout the max value among three given values

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
a=1;
b=2,c=3;
d=(a>b?(a>c?a:c):(b>c?b:c));
printf("%d",d);
getch();
}

output:-
3