The Conditional Operator in C Programming Language
The conditional operator in C 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 for conditional operator in C:
1 |
condition ? result1 : result2; |
If the condition is true, result1 is returned else result2 is returned. C Conditional Operator Examples:
1 2 3 |
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. |
Program in C using Conditional Operator
1 2 3 4 5 6 7 8 9 10 |
/*Conditional operator*/ #include #include void main() { int a = 10, b = 11; int c; c = (a < b)? a : b; printf(“%d”, c); } |
Output: 10 In the above program, if the value of a is less than b then the value of a would be assigned to...