C - Conditional Operator

The conditional operator in C, often referred to as the "ternary operator," is a special operator used to create concise conditional expressions. It allows you to make decisions and choose one of two possible values or expressions based on the evaluation of a condition. The syntax of the conditional operator is as follows:


condition ? expression_if_true : expression_if_false;

Here's a breakdown of the components of the conditional operator:

  • 'condition': This is a Boolean expression or a condition that is evaluated to either true or false.
  • 'expression_if_true': If the condition is true (non-zero), this expression is evaluated and becomes the result of the conditional operator.
  • 'expression_if_false': If the condition is false (zero), this expression is evaluated and becomes the result of the conditional operator.

Key points about the conditional operator:

  • It is a ternary operator because it takes three operands: condition, expression_if_true, and expression_if_false.
  • It provides a concise way to write conditional statements without using if and else.
  • It is often used for simple conditional assignments, where you choose one value or another based on a condition.

Let's illustrate the conditional operator step by step with an example.

Suppose we want to determine whether a given number x is positive or negative, and we want to store the result in a variable called result. We can use the conditional operator to achieve this.

Here's the example code:


#include <stdio.h>

int main() {
    int x = -5; // The number we want to check
    int result;

    // Using the conditional operator to check if x is positive or negative
    result = (x >= 0) ? 1 : 0;

    // Display the result
    if (result) {
        printf("The number is positive.\n");
    } else {
        printf("The number is negative.\n");
    }

    return 0;
}

Now, let's break down how the conditional operator works in this example:

  1. We declare an integer variable x and initialize it with the value -5, which is a negative number.
  2. We declare an integer variable result where we will store the result of whether x is positive or negative.
  3. We use the conditional operator (x >= 0) ? 1 : 0; to evaluate whether x is greater than or equal to 0. Here's how it works:
    • The condition x >= 0 checks if x is greater than or equal to 0. In this case, it is false because -5 is not greater than or equal to 0.
    • Since the condition is false, the expression after the colon (0) is evaluated.
  4. The result of the conditional operator is 0, which means x is considered negative.
  5. We use an if statement to display the result. If result is 1, we print "The number is positive," and if result is 0, we print "The number is negative."

When you run this program, you will get the following output:


The number is negative.

In this example, we've used the conditional operator to make a decision based on whether x is positive or negative, and we've stored the result in the result variable. This demonstrates how the conditional operator can be used to simplify conditional assignments in C.