Conditional Ternary Operator (? :) in C: Syntax, Examples & Rules
BCA Sem 1 | Unit 2 | Computer Concepts & C Programming
Conditional Ternary Operator (? :) in C — BKNMU Junagadh
Syntax, Short-hand Decision Making, Nested Ternary Operators, and Comparison with If-Else
? :) is the only ternary operator in C language because it takes three operands. It serves as a compact shorthand for simple if-else conditions, allowing inline value assignment based on a logical test.
The conditional operator consists of three distinct parts separated by ? and : symbols:
1. Test Expression
The boolean expression evaluated first. Evaluates to True (non-zero) or False (zero).
2. True Branch
The expression executed or returned if the condition evaluates to True (non-zero).
3. False Branch
The expression executed or returned if the condition evaluates to False (zero).
4. Nested Ternary
Placing another ternary operator inside the True or False branch for multi-level checks.
Here is a detailed comparative summary between the Conditional Operator and the traditional if-else statement:
| Feature | Ternary Operator (? :) | If-Else Statement (if-else) |
|---|---|---|
Operator Type | Ternary (operates on 3 operands). | Control flow statement. |
Value Returning | Returns a value directly; can be used on the right side of an assignment. | Does NOT return a value directly; requires assignment inside code blocks. |
Syntax Compactness | Ultra-compact, single-line expression format. | Multi-line structured block statement format. |
Complexity | Best suited for simple binary conditions and quick assignments. | Handles multi-statement blocks, nested conditions, and complex control flow logic cleanly. |
++), Binary (2 operands like +, &&), and Ternary (3 operands: Condition ? Exp1 : Exp2).
1. Basic Ternary Example (Find Maximum of Two Numbers):
#include <stdio.h>
int main() {
int a = 15, b = 20;
int max;
// Ternary operator inline assignment
max = (a > b) ? a : b;
printf("The maximum value is: %d\n", max);
return 0;
}
2. Checking Even or Odd in a Single Line:
#include <stdio.h>
int main() {
int num = 7;
// Direct printing using ternary operator
(num % 2 == 0) ? printf("%d is Even\n", num) : printf("%d is Odd\n", num);
return 0;
}
3. Nested Ternary Example (Largest of Three Numbers):
#include <stdio.h>
int main() {
int a = 10, b = 25, c = 15;
int largest;
// Nested ternary evaluation
largest = (a >= b) ? ((a >= c) ? a : c) : ((b >= c) ? b : c);
printf("Largest number is: %d\n", largest);
return 0;
}
Evaluate Test Expression
The expression before the ? symbol is evaluated first as a logical test.
Execute True Branch (If Non-Zero)
If the condition is True (non-zero), Expression 1 (between ? and :) is evaluated and returned.
Skip False Branch (or Vice Versa)
Expression 2 (after :) is completely skipped if True. If False (zero), Expression 1 is skipped and Expression 2 executes.
variable = (condition) ? value_if_true : value_if_false;
else-if ladder for complex multiple conditions instead.
- Operator Type: Only ternary operator in C (operates on 3 operands).
- Symbols Used: Question mark (
?) and Colon (:). - Value Return: Unlike
if-else, ternary expressions return a resulting value directly. - Primary Use Case: Compact single-line conditional assignments and return statements.
- Precedence: Has lower precedence than arithmetic and relational operators.