---
title: "Operators; conditional operator"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:operator-conditional"
source: https://www.bctengineer.com/q/semester_1/computer_programming/operator-conditional
site: BCT Engineering
---

# Operators; conditional operator

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2073 Chaitra, 2+3 marks
- Video: [Conditional Operator in C](https://www.youtube.com/watch?v=rULDbIbrXis) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/operator-conditional
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

What is operator? Describe the conditional operator in C with syntax and example.

## Short answer

The conditional operator (? :) is a ternary operator: condition ? expr1 : expr2. It evaluates to expr1 if condition is true, else expr2. Example: max = (a>b) ? a : b assigns the larger value.

## Answer

An **operator** is a symbol that tells the compiler to perform a specific operation (arithmetic, logical, relational, etc.) on one or more operands. For example, in `a + b` the symbol `+` is the operator and `a`, `b` are operands.

#### Conditional operator (`? :`)
It is the only ternary operator in C (it takes three operands) and works like a short if-else.

**Syntax:** `variable = (condition) ? expression1 : expression2;`

If the condition is true, `expression1` is evaluated; otherwise `expression2` is evaluated.

**Example:**
```c
#include <stdio.h>
int main(void)
{
    int a = 12, b = 20, max;
    max = (a > b) ? a : b;
    printf("Maximum = %d\n", max);
    printf("%s\n", (a % 2 == 0) ? "a is even" : "a is odd");
    return 0;
}
```

Output:
```text
Maximum = 20
a is even
```

Here `(a > b) ? a : b` gives 20 as the larger value, and the second statement chooses between two messages.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is the conditional operator called ternary?
   A. It works on three operands
   B. It has three symbols
   C. It repeats three times
   D. It returns three values
   Answer: A. ?: takes three operands: condition, expression1 and expression2. (Key point: Ternary)

2. Which is the correct syntax of the conditional operator?
   A. variable = expression1 ? condition : expression2;
   B. variable = (condition) ? expression1 : expression2;
   C. variable = (condition) : expression1 ? expression2;
   D. variable = condition ? expression1, expression2;
   Answer: B. The condition comes first, then ? and the true value, then : and the false value. (Key point: Syntax)

3. With a=12, b=20, what does max = (a > b) ? a : b; store?
   A. 12
   B. 20
   C. 1
   D. 0
   Answer: B. The condition is false, so expression2 (b = 20) is evaluated. (Key point: Evaluation)

## Related in Computer Programming

- [Conditional operator vs if-else; digit sum](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aconditional-operator-vs-ifelse) — Computer Programming, chapter 5
- [Global vs local; & and * operators](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aglobal-local-ampersand-star) — Computer Programming, chapter 3

## Related in other subjects

- [Syntax of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-syntax) — Object Oriented Programming, chapter 5, asked 4×
- [Regression: operator experience and performance](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Aexperience-performance-regression-r2) — Probability and Statistics, chapter 5
- [Binary and unary operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-unary-overloading) — Object Oriented Programming, chapter 5
- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5
