Conditional Operator in C
In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is given below:
Syntax:
condition ? expression1: expression2
If the condition is true, the operator returns expression1, otherwise it returns expression2.
Here is an example:
int x = 5, y = 10;
int min = (x < y) ? x : y;
In this example, the variable min will be assigned the value of x (5) because 5 is less than 10.
It can also be used in an expression as:
printf("%d", (x>y)? x:y);
It will print the value of x if x>y else y.
This statement is equivalent to the following if-else statement:
if (a > b) {
larger = a;
} else {
larger = b;
}
Note: It is important to note that the ternary operator can only return one value and the type of the expressions on both side of the : operator should be same.
Working of Conditional operator in C
The conditional operator in C works by evaluating the condition specified before the ? operator. If the condition is true, the expression following the ? operator is executed and its value is returned. If the condition is false, the expression following the : operator is executed and its value is returned.
Here is the step-by-step process of how the conditional operator works in the example we provided earlier:
- The program declares an int variable age and assigns it the value of 20.
- The program declares a pointer to char variable status and assigns it the value of "adult" if the value of age is greater than or equal to 21, or "minor" if the value of age is less than 21.
- The program evaluates the condition age >= 21 which is false in this case.
- Since the condition is false, the expression "minor" following the : operator is executed.
- The value of "minor" is returned and assigned to the variable status.
- The program then prints the value of status using the printf() function.
It is important to note that the conditional operator is a shorthand way of writing an if-else statement, and it can be used to assign a value to a variable, execute a statement or return a value.
Here is an example of using the conditional operator in a program.
Example:
#include <stdio.h>
int main() {
int age = 20;
char *status;
status = (age >= 21) ? "adult" : "minor";
printf("You are a %s\n", status);
return 0;
}
Output:
You are a minor
In this example, the program first declares an int variable age and assigns it the value of 20. It then declares a pointer to char variable status and assigns it the value of "adult" if the value of age is greater than or equal to 21, or "minor" if the value of age is less than 21. The program then prints the value of status using the printf() function.
Difference between Conditional Operator in C and if-else Statement
The conditional operator (? :) and the if-else statements are both used to make decisions in C, but they have some key differences:
- Syntax: The conditional operator has a shorthand syntax, which is often more concise than an if-else statement. The if-else statement has a more verbose syntax that requires the use of curly braces {} and the keywords if, else, and else if.
- Evaluation: The conditional operator evaluates a single condition and returns a value or executes a single statement based on the outcome of that condition. On the other hand, an if-else statement can evaluate multiple conditions using else if and can execute multiple statements based on the outcome of those conditions.
- Type of returned value: The conditional operator can only return one value and the type of the expressions on both side of the : operator should be same. The if-else statement doesn't return any value.
- Nesting: The conditional operator cannot be nested within another conditional operator or if-else statement. On the other hand, if-else statements can be nested within other if-else statements to create more complex decision-making logic.
- Execution of statement: The conditional operator will execute only one statement, whereas if-else statement can execute multiple statements based on the conditions.
In general, the conditional operator is best used for simple decisions that only require a single condition to be evaluated, and it is often used to assign a value to a variable based on that condition. The if-else statement is more powerful and flexible, and it is best used for more complex decision-making logic that requires multiple conditions to be evaluated and multiple statements to be executed based on those conditions.