Conditional Operator in C

Conditional Operator in C. Conditional Operator in C Tutorial. Learn Conditional Operator in C
Conditional Operator in C.

What is Conditional Operator in C?

The Conditional Operator in C is a ternary operator that evaluates a Boolean expression and returns one of two expressions based on the result of the evaluation. It is also known as the ternary operator because it takes three operands, unlike other operators that take two operands.

Syntax:

(condition) ? expression1 : expression2;

Here, condition is a Boolean expression that is evaluated. If the result of condition is true, expression1 is executed, otherwise expression2 is executed.

Examples of Conditional Operator in C:

Example 1:

Checking which number is greater.

#include <stdio.h>
int main() {
    int x = 5, y = 10, max;
    max = (x > y) ? x : y;
    printf("The maximum number is %d", max);
    return 0;
}

Output:

The maximum number is 10

Explanation:

Here, we compare x and y using the conditional operator. If x is greater than y, the value of x is assigned to max. Otherwise, the value of y is assigned to max. In this case, y is greater than x, so max is assigned the value of y, which is 10.

Example 2:

Checking if the number is Even or Odd:

#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    (num % 2 == 0) ? printf("Even number") : printf("Odd number");
    return 0;
}

Output 1:

Enter a number: 6
Even number

Output 2:

Enter a number: 7
Odd number

Explanation:

In above example, we take an input from the user and use the conditional operator to check if the input is even or odd. If the input is even, the message “Even number” is printed, otherwise the message “Odd number” is printed.

Rules to follow while using Conditional Operator in C:

  1. The condition must be a Boolean expression.
  2. The expressions returned by the operator must have the same data type.
  3. The conditional operator has a lower precedence than the arithmetic and logical operators. Parentheses should be used to clarify the order of evaluation if necessary.

Modifications that can be made while using the Conditional Operator in C:

  • The expressions returned by the operator can be function calls, variable assignments, or any valid expression.
  • The operator can be used to simplify if-else statements with a single statement in each block. For example, instead of writing:
if (x > y) {
    z = x;
} else {
    z = y;
}
  • We can write like this:
z = (x > y) ? x : y;
  • The conditional operator can be used to initialize variables. For example:
int x = (y > 0) ? y : 0;
  • Here, if y is greater than zero, x is assigned the value of y. Otherwise, x is assigned the value of zero.
  • The conditional operator can be used in conjunction with the comma operator to execute multiple expressions in a single statement. For example:
int x = (y > 0) ? (z++, y) : (w++, z);
  • Here, if y is greater than zero, z is incremented and the value of y is assigned to x. Otherwise, w is incremented and the value of z is assigned to x.

More: