C Programming: Conditional (Ternary) Operator
Conditional Operator in C Programming
Overview
In C, the conditional operator ?: is a shorthand for an if-else statement. It is called the ternary operator because it operates on three expressions:
Exp1 ? Exp2 : Exp3;
- Exp1: The condition to evaluate.
- Exp2: The result if Exp1 is true (non-zero).
- Exp3: The result if Exp1 is false (zero).
The ternary operator allows for more concise code, especially in simple conditional expressions.
Syntax:
Exp1 ? Exp2 : Exp3;
- If Exp1 evaluates to true (non-zero), Exp2 is executed.
- If Exp1 evaluates to false (zero), Exp3 is executed.
Key Topics:
- The conditional operator ?: is a compact way of writing an if-else statement.
- It is useful when you need to assign values based on a condition in a single line.
- Only one of the two expressions (Exp2 or Exp3) is evaluated based on the condition.
Example 1: Basic Conditional Operator usage
The conditional operator compares two values and assigns the larger one to a variable.
Code:
#include <stdio.h>
int main() {
int a = 10, b = 20;
// Use of the conditional operator
int max = (a > b) ? a : b; // If 'a' is greater than 'b', assign 'a' to 'max', else assign 'b'
printf("The maximum value is: %d\n", max); // Output will be 20
return 0;
}
Output:
The maximum value is: 20.
Explanation:
- The conditional operator checks if a > b.
- Since this condition is false (10 is not greater than 20), the value of b (20) is assigned to max.
Example 2: Checking for Even or Odd
The conditional operator checks whether a number is even or odd and returns the corresponding result.
Code:
#include <stdio.h>
int main() {
int num = 5;
// Check if the number is even or odd using the conditional operator
const char *result = (num % 2 == 0) ? "Even" : "Odd"; // If 'num' is divisible by 2, it's even; otherwise, it's odd
printf("%d is %s\n", num, result); // Output will be 5 is Odd
return 0;
}
Output:
5 is Odd.
Explanation:
- The conditional operator checks if num % 2 == 0 (whether the number is even).
- Since 5 is not divisible by 2, the string "Odd" is assigned to result.
Example 3: Simplifying Multiple Conditional assignments
Nested conditional operators allow checking for multiple conditions concisely.
Code:
#include <stdio.h>
int main() {
int num = -10;
// Determine if the number is positive, negative, or zero
const char *result = (num > 0) ? "Positive" : ((num < 0) ? "Negative" : "Zero"); // Nested conditional operators
printf("%d is %s\n", num, result); // Output will be -10 is Negative
return 0;
}
Output:
-10 is Negative.
Explanation:
- The outer conditional checks if num > 0. Since this is false, it moves to the inner conditional.
- The inner conditional checks if num < 0. Since this is true, "Negative" is assigned to result.
Example 4: Conditional Operator in Function return
The conditional operator can be used in functions to return values based on conditions.
Code:
#include <stdio.h>
// Function to return the smaller of two numbers using the conditional operator
int min(int a, int b) {
return (a < b) ? a : b; // If 'a' is less than 'b', return 'a', else return 'b'
}
int main() {
int x = 15, y = 25;
printf("The minimum value is: %d\n", min(x, y)); // Output will be 15
return 0;
}
Output:
The minimum value is: 15.
Explanation:
- The function min uses the conditional operator to return the smaller of the two arguments.
- Since 15 is less than 25, min(x, y) returns 15.
Example 5: Conditional Assignment in expressions
The conditional operator can be used within expressions to simplify logic.
Code:
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Use the conditional operator directly in an expression
int result = (a < b) ? (a + b) : (a - b); // If 'a' is less than 'b', result is 'a + b', else it's 'a - b'
printf("Result: %d\n", result); // Output will be 15
return 0;
}
Output:
Result: 15.
Explanation:
- The condition a < b is true, so the expression a + b is evaluated.
- Since a is 5 and b is 10, the result is 15.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/c-conditional-operator.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics