Ternary Operator in C
Often you need to pick one of two values based on a condition: the bigger of two numbers, "Pass" or "Fail", "even" or "odd". Writing a full if-else for this takes 4 lines.
The ternary operator ? : does it in one line. In this lesson you will learn its syntax and use it to find the largest of two and three numbers, a very common exam program.
What You Will Learn
- The syntax of the ternary (conditional) operator.
- How it chooses between two values.
- Largest of two and three numbers without
if. - Nested ternary operators.
- When to use ternary and when to use if-else.
Ternary Operator in C Explained
Exam definition: The conditional (ternary) operator ? : takes three operands. It checks a condition and returns the first value if the condition is true, otherwise the second value. It is the only ternary operator in C.
- Ternary means "three parts": the condition, the true value and the false value.
- It returns a value, so you can store it, print it, or use it inside another expression.
Memory trick: Read a > b ? a : b as a question: "Is a greater than b? If yes, a. Otherwise, b." The ? asks the question, the : means "otherwise".
Syntax of the Ternary Operator
condition ? value_if_true : value_if_false;
max = (a > b) ? a : b;
This one line does the same job as:
if (a > b)
max = a;
else
max = b;
Example 1: Largest of Two Numbers Without if
#include <stdio.h>
int main(void)
{
int a, b, max;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
max = (a > b) ? a : b;
printf("Largest = %d\n", max);
return 0;
}
Output
Enter first number: 18 Enter second number: 25 Largest = 25
Code Explanation
(a > b): is 18 > 25? No, it is false.- Because the condition is false, the value after
:is chosen:b(25). - 25 is stored in
maxand printed.
How the Ternary Operator Works
- C checks the condition first.
- If it is true (non-zero), it evaluates only the first value.
- If it is false (0), it evaluates only the second value.
- The chosen value becomes the result of the whole expression.
- It has very low precedence and works right to left, so
a ? b : c ? d : emeansa ? b : (c ? d : e). Use brackets to keep it readable.
Example 2: Largest of Three Numbers Using Nested Ternary
#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
int largest = (a > b) ? ((a > c) ? a : c)
: ((b > c) ? b : c);
printf("Largest = %d\n", largest);
printf("%d is %s\n", largest, (largest % 2 == 0) ? "even" : "odd");
return 0;
}
Output
Enter three numbers: 12 45 30 Largest = 45 45 is odd
First compare a and b. If a is bigger, compare a with c; otherwise compare b with c. Here 12 > 45 is false, so it checks b > c: 45 > 30 is true, giving 45.
Important Points to Remember
- Syntax:
condition ? true_value : false_value. - It is the only ternary operator (3 operands).
- It returns a value, unlike
if, which is a statement. - Only one of the two values is evaluated.
- Use brackets with nested ternaries; avoid nesting too deep.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
a > b ? a (no : part) | Syntax error | Both parts are required |
Swapping the values: a > b ? b : a for max | Gives the minimum | True value comes right after ? |
| Deeply nested ternaries on one line | Hard to read and debug | Use if-else for complex logic |
Using it just to call functions: x ? f() : g(); | Works but confusing | Use if-else for actions |
Ternary vs if-else
| Ternary operator | if-else |
|---|---|
| An expression: returns a value | A statement: does actions |
| One line | Several lines |
| Best for choosing between two values | Best for many statements or complex logic |
| Can be used inside printf or an assignment | Cannot be used inside an expression |
Ternary Operator Practice Questions
- What is the ternary operator? Why is it called ternary?
- Write the syntax of the conditional operator.
- Write one statement to find the smaller of two numbers
xandy. - What is the output of
printf("%s", 7 % 2 ? "odd" : "even");? - Write two differences between the ternary operator and if-else.
Show answers
- A conditional operator
? :that returns one of two values based on a condition. It is called ternary because it has three operands. condition ? expression1 : expression2;min = (x < y) ? x : y;odd(7 % 2 is 1, which is true).- Ternary is an expression that returns a value and fits in one line; if-else is a statement used for actions and complex logic.
Coding Exercise
Read marks (0–100) and print "Pass" if marks are 40 or more, otherwise "Fail", using the ternary operator inside printf.
Show solution
#include <stdio.h>
int main(void)
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
printf("Result: %s\n", marks >= 40 ? "Pass" : "Fail");
return 0;
}
Enter marks: 38 Result: Fail
Quick Quiz: Ternary Operator in C (MCQ)
-
How many operands does the conditional operator take?
A) 1 B) 2 C) 3 D) 4
-
Output of
printf("%d", 5 > 3 ? 10 : 20);A) 5 B) 10 C) 20 D) 1
-
Which finds the maximum of a and b?
A)
a < b ? a : bB)a > b ? a : bC)a > b : a ? bD)a ? b : a > b -
The ternary operator's associativity is:
A) Left to right B) Right to left C) None D) Depends on compiler
-
Output of
printf("%d", 0 ? 1 : 2);A) 0 B) 1 C) 2 D) Error
Show answers
- C: 3.
- B: 10.
- B:
a > b ? a : b - B: right to left.
- C: 2 (0 is false).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Symbol | ? : |
|---|---|
| Syntax | condition ? true_value : false_value |
| Operands | 3 (only ternary operator) |
| Max of a, b | a > b ? a : b |
| Associativity | Right to left |
| Returns | A value (it is an expression) |
Frequently asked questions
- Why is ? : called the ternary operator?
- Because it has three operands: condition, true value and false value.
- Can the ternary operator replace if-else?
- Only for choosing between two values; for actions or complex logic, use if-else.
- Are both values evaluated?
- No. Only the chosen value is evaluated.
- How do you find the largest of three numbers in one line?
(a > b) ? (a > c ? a : c) : (b > c ? b : c)
Remember
- ? asks the question, : means otherwise.
- True value comes right after the ?.
Next Topic
Next lesson: sizeof and Comma Operators in C. You will learn two special operators: one that measures memory and one that joins expressions.