Back to C Programming
2026-07-126 min read

C - Ternary Operator

Learn C - Ternary Operator step by step with clear examples and exercises.

Why This Matters

In this lesson, we'll delve into one of C programming's most efficient and concise tools: the ternary operator. This operator serves as a shorthand for if-else statements, making your code cleaner and more readable. Let's explore its uses and benefits!

Prerequisites

Before diving into the ternary operator, it's essential to have a solid understanding of the following concepts:

  • C programming syntax basics
  • Control structures (if, while, for)
  • Variables and data types

If you're new to these topics, I recommend reviewing our previous lessons covering them.

Core Concept

The ternary operator is a powerful alternative to traditional if-else statements in C. It consists of three parts:

  1. A condition enclosed in parentheses
  2. The expression to be executed if the condition is true
  3. The expression to be executed if the condition is false

The syntax for the ternary operator looks like this:

(condition) ? expression_if_true : expression_if_false;

When the condition evaluates to true, the program executes the expression following the ?. If the condition is false, it executes the expression following the :. The whole ternary operator then returns the value of the last expression.

Here's an example that demonstrates how the ternary operator works:

#include <stdio.h>

int main() {
int num = 10;
int max = (num > 5) ? 20 : 30;
printf("The maximum value is %d\n", max);
return 0;
}

In this example, we're checking if num is greater than 5. If it is, the expression 20 is executed and assigned to the variable max. Otherwise, the expression 30 is executed instead. The output of the program will be:

The maximum value is 20

Ternary Operator vs. Conditional Compilation

Note that that the ternary operator and conditional compilation (using #ifdef and #endif) serve different purposes. The ternary operator is used for conditional execution of expressions, while conditional compilation is used to include or exclude code based on preprocessor directives.

Worked Example

Let's walk through a more complex example that demonstrates how you can use the ternary operator to simplify code and make it easier to read. Consider the following if-else statement:

int x = 10;
int y = 20;
int z = 15;
int max;

if (x > y && x > z) {
max = x;
} else if (y > x && y > z) {
max = y;
} else {
max = z;
}
printf("The maximum value is %d\n", max);

Now, let's rewrite this using a ternary operator:

int x = 10;
int y = 20;
int z = 15;
int max = (x > y && x > z) ? x : ((y > x && y > z) ? y : z);
printf("The maximum value is %d\n", max);

Notice that we've replaced the if-else statement with a single line of code using the ternary operator. This makes the code more concise and easier to understand at a glance.

Nested Ternary Operators

In some cases, you may need to use nested ternary operators to handle complex conditions. Here's an example:

int x = 10;
int y = 20;
int z = 15;
int max = (x > y) ? (x > z ? x : z) : (y > z ? y : z);
printf("The maximum value is %d\n", max);

In this example, we've nested two ternary operators to find the maximum of three numbers. The outer operator checks if x is greater than y, and if so, the inner operator determines whether x or z is larger. If x is not greater than y, the inner operator checks if y is greater than z, and if so, returns y.

Common Mistakes

  1. Forgotten semicolon: Remember to include a semicolon after the ternary operator, just like any other statement in C.
// Incorrect: (condition) ? expression_if_true : expression_if_false;
int max = (num > 5) ? expression_if_true : expression_if_false;

// Correct: (condition) ? expression_if_true ; expression_if_false;
int max = (num > 5) ? expression_if_true ; expression_if_false;
  1. Incorrect order of expressions: Ensure that the expressions following ? and : are in the correct order, with the expression for true first and the expression for false second.
// Incorrect: (condition) ? expression_if_false : expression_if_true;
int max = (num > 5) ? expression_if_false : expression_if_true;

// Correct: (condition) ? expression_if_true : expression_if_false;
int max = (num > 5) ? expression_if_true : expression_if_false;
  1. Forgetting to assign the result: The ternary operator returns a value, so make sure you're actually using that value in your code.
// Incorrect: (condition) ? expression_if_true : expression_if_false;
(num > 5) ? expression_if_true : expression_if_false;

// Correct: int max = (condition) ? expression_if_true : expression_if_false;
int max = (num > 5) ? expression_if_true : expression_if_false;
  1. Misunderstanding the return type: The ternary operator always returns a value, so if you're using it in a context where a value is expected, such as a function or an assignment, it should be fine. However, if you're using it in a context where no value is expected, like within a void function or inside a control structure that doesn't return a value (e.g., if, while, for), you may encounter errors.
// Incorrect: void function() {
// (condition) ? expression_if_true : expression_if_false;
// }
void function() {
int max = (condition) ? expression_if_true : expression_if_false;
}
  1. Confusing the ternary operator with assignment: The ternary operator is an expression, not an assignment. If you want to assign a value based on a condition, use the if-else statement or the conditional assignment operator (?:=).
// Incorrect: int x = (condition) ? expression_if_true : expression_if_false;
int x = (condition) ? expression_if_true = expression_if_true2 : expression_if_false;

// Correct using assignment operator:
int x;
x = (condition) ? expression_if_true : expression_if_false;

// Correct using if-else statement:
int x;
if (condition) {
x = expression_if_true;
} else {
x = expression_if_false;
}

Practice Questions

  1. Write a program that uses the ternary operator to find the maximum of three numbers.
  2. Rewrite the following if-else statement using a ternary operator:
int x = 5;
int y = 10;
int z = 15;
int max;

if (x > y && x > z) {
max = x;
} else if (y > x && y > z) {
max = y;
} else {
max = z;
}
printf("The maximum value is %d\n", max);
  1. Write a program that uses the ternary operator to find the larger of two numbers, and also prints whether or not they are equal.
  2. Rewrite the following if-else if chain using nested ternary operators:
int x = 10;
int y = 20;
int z = 30;
int max;

if (x > y) {
max = x;
} else if (y > z) {
max = y;
} else {
max = z;
}
printf("The maximum value is %d\n", max);
  1. Write a program that uses the ternary operator to determine if a number is even or odd, and also prints the number itself.

FAQ

  1. Can I use multiple conditions in a single ternary operator?
  • No, the ternary operator only allows for one condition at a time. If you need to check multiple conditions, consider using nested if-else statements or logical operators like && and ||.
  1. Can I use the ternary operator in function calls?
  • Yes, just like any other expression, you can use the ternary operator within function calls. For example:
int max = (num1 > num2) ? func1(num1) : func2(num2);
  1. Is it always better to use a ternary operator instead of an if-else statement?
  • Not necessarily. The ternary operator can make your code more concise and easier to read in some cases, but it may not be the best choice for complex or multi-line conditions. Use your judgment to determine when each is most appropriate.
  1. Can I use the ternary operator with array indices?
  • Yes, you can use the ternary operator with array indices as long as the index is an integer and within the valid range of the array. For example:
int arr[5] = {1, 2, 3, 4, 5};
int idx = 3;
int value = (idx >= 0 && idx < 5) ? arr[idx] : -1;
printf("The value at index %d is %d\n", idx, value);

In this example, we're using the ternary operator to check if the index idx is within the valid range of the array arr, and if so, returning the corresponding value. If the index is out of bounds, the operator returns -1.