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
c coding samples to programmers
ReplyDeleteSquare Root of a number by using simple calculations