The Conditional Operator in C Language




The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.

Syntax:
condition ? result1 : result2

If the condition is true, result1 is returned else result2 is returned.

Examples:

10==5 ? 11: 12 // returns 12, since 10 not equal to 5.
10!=5 ? 4 : 3 // returns 4, since 10 not equal to 5.
12>8 ? a : b // returns the value of a, since 12 is greater than 8.

/*Conditional operator*/
#include
#include
void main(){
int a = 10, b = 11;
int c;
c = (a < b)? a : b
printf(“%d”, c);
}

Output:
10


Responses

0 Respones to "The Conditional Operator in C Language"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy