---
title: 3.21 Conditional / Ternary Operator
description: "The conditional operator `?:` provides a compact way to select one of two expressions based on a condition. ``` #include int main() { int a = 10; int b = 20; int greater; greater = (a > b) ? a…"
url: "https://edcret.in/docs/c-programming/operators-and-expressions/3-21-conditional-ternary-operator/"
updated: "2026-09-07"
category: C Programming / Operators and Expressions
---

# 3.21 Conditional / Ternary Operator

The conditional operator `?:` provides a compact way to select one of two expressions based on a condition.

```
#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;
    int greater;

    greater = (a > b) ? a : b;

    printf("Greater value = %dn", greater);

    return 0;
}
```

### Example Output

```
Greater value = 20
```

### Explanation

The conditional operator has three operands:

```
condition ? expression1 : expression2
```

In the example:

```
(a > b) ? a : b
```

`a > b` is the condition. Since `10 > 20` is false, the expression after `:` is selected:

```
b
```

Therefore, `greater` receives the value `20`.

Only one of the two result expressions is evaluated.
