C Ternary Operator

In C, the ternary operator ?: is a conditional operator that provides a shorthand way to write an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false.

The ternary operator is useful for writing concise and efficient conditional expressions.

In this tutorial, we will learn its syntax, usage and nesting of ternary operators with examples.

Flow Diagram of C Ternary Operator

Following is the flow diagram of Ternary Operator in C.

C Ternary Operator

Syntax of C Ternary Operator

The syntax to use the ternary operator is:

</>
Copy
condition ? expression1 : expression2;

Explanation:

  1. condition: The expression that is evaluated. If true, expression1 executes; otherwise, expression2 executes.
  2. ?: Separates the condition from the true expression.
  3. expression1: Executes if the condition is true.
  4. :: Separates the true expression from the false expression.
  5. expression2: Executes if the condition is false.

Ternary Operator can be interpreted using if-else statement as below.

</>
Copy
if (condition) {
    x = expression1;
} else {
    x = expression2;
}

Examples of the Ternary Operator

1. Checking if a Number is Even or Odd

In this example, we will use the ternary operator to determine whether a number is even or odd.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 7;

    // Using ternary operator to check even or odd
    (num % 2 == 0) ? printf("Even\n") : printf("Odd\n");

    return 0;
}

Explanation:

  1. We declare an integer variable num and assign it the value 7.
  2. The condition num % 2 == 0 checks whether num is divisible by 2.
  3. If true, printf("Even\n") executes.
  4. If false, printf("Odd\n") executes.

Output:

Odd

2. Finding the Maximum of Two Numbers

In this example, we use the ternary operator to find the maximum of two numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 10, b = 20;
    
    // Using ternary operator to find the maximum number
    int max = (a > b) ? a : b;

    printf("Maximum: %d\n", max);

    return 0;
}

Explanation:

  1. We declare two integer variables a and b, initialized to 10 and 20.
  2. The condition a > b checks if a is greater than b.
  3. If true, max is assigned a.
  4. If false, max is assigned b.
  5. The maximum value is printed using printf().

Output:

Maximum: 20

3. Checking if a Number is Positive or Negative

In this example, we use the ternary operator to check whether a number is positive or negative.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = -5;

    // Using ternary operator to check positive or negative
    (num >= 0) ? printf("Positive\n") : printf("Negative\n");

    return 0;
}

Explanation:

  1. We declare an integer variable num and assign it the value -5.
  2. The condition num >= 0 checks if the number is positive or zero.
  3. If true, printf("Positive\n") executes.
  4. If false, printf("Negative\n") executes.

Output:

Negative

Conclusion

In this tutorial, we covered the ternary operator ?: in C. The important points to remember are:

  1. The ternary operator provides a concise way to write conditional expressions.
  2. It evaluates a condition and returns one of two values based on the result.
  3. It is useful for simple decision-making in C programs.