BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Conditional Ternary Operator (? :) in C: Syntax, Examples & Rules

by BCA School



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

BKNMU Junagadh NEP 2020 Unit 2 Ternary Operator Inline Conditions C Operators
In simple words: Welcome to Unit 2! The Conditional Operator (represented by ? :) 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.
📖 Anatomy of the Ternary Operator

The conditional operator consists of three distinct parts separated by ? and : symbols:

Condition

1. Test Expression

The boolean expression evaluated first. Evaluates to True (non-zero) or False (zero).

Expression 1

2. True Branch

The expression executed or returned if the condition evaluates to True (non-zero).

Expression 2

3. False Branch

The expression executed or returned if the condition evaluates to False (zero).

Nested (? :)

4. Nested Ternary

Placing another ternary operator inside the True or False branch for multi-level checks.

1️⃣ Master Table: Ternary Operator vs. If-Else

Here is a detailed comparative summary between the Conditional Operator and the traditional if-else statement:

FeatureTernary Operator (? :)If-Else Statement (if-else)
Operator TypeTernary (operates on 3 operands).Control flow statement.
Value ReturningReturns 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 CompactnessUltra-compact, single-line expression format.Multi-line structured block statement format.
ComplexityBest suited for simple binary conditions and quick assignments.Handles multi-statement blocks, nested conditions, and complex control flow logic cleanly.
ℹ️ Why is it called "Ternary"? In C language, operators are classified by operand count: Unary (1 operand like ++), Binary (2 operands like +, &&), and Ternary (3 operands: Condition ? Exp1 : Exp2).
💻 Code Examples & Practical Applications

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;
}
🔄 Step-by-Step Evaluation Process
1

Evaluate Test Expression

The expression before the ? symbol is evaluated first as a logical test.

2

Execute True Branch (If Non-Zero)

If the condition is True (non-zero), Expression 1 (between ? and :) is evaluated and returned.

3

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.

💡 Exam Tip on Syntax: Always remember the basic syntax formula for GTU/BKNMU exam papers: variable = (condition) ? value_if_true : value_if_false;
⚠️ Code Readability Pitfall: Avoid over-nesting ternary operators (e.g., nesting 3 or 4 levels deep). While valid C syntax, it makes code extremely difficult to read and debug! Use an else-if ladder for complex multiple conditions instead.
📝 Quick Revision — Key Exam Points
  • 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.