DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 28 of 64
←PreviousPrevNextNext→

C Short Hand If (Ternary Operator)

Introduction to the Ternary Operator

The ternary operator (?:) is a conditional operator that provides a shorthand way to write simple if-else statements. It is the only operator in C that takes three operands, hence the name 'ternary'.

This operator is useful for concise conditional assignments and expressions where a full if-else statement would be too verbose.

Important: it is an expression that produces a value (and can be used wherever a value is allowed), not a statement.

Syntax and Structure

The syntax of the ternary operator is:

condition ? expression_if_true : expression_if_false

The condition is evaluated first. If true, the first expression is evaluated and returned. If false, the second expression is evaluated and returned.

Precedence and Associativity

The ternary operator has lower precedence than logical operators (&&, ||) and higher precedence than assignment (=). It associates right-to-left, which affects how nested ternaries are grouped.

When in doubt, add parentheses for clarity.

Example
#include <stdio.h>

int main(void) {
    int a = 3, b = 5, x;
    // Precedence: assignment happens after ?:, so this is clear
    x = (a > b) ? a : b; // x = 5

    // Associativity: right-to-left
    int n = 0;
    const char *label = (n > 0) ? "Positive" : (n < 0) ? "Negative" : "Zero";
    printf("x=%d, label=%s\n", x, label);

    // Prefer parentheses for readability with assignments inside branches
    int y = 0;
    (a > b) ? (y = a) : (y = b);
    printf("y=%d\n", y);
    return 0;
}
Output
x=5, label=Zero
y=5
ℹ️ Note: Because ?: binds tighter than =, expressions like cond ? x = 1 : x = 2 parse as cond ? (x = 1) : (x = 2). Parentheses improve readability.

Basic Examples

Example
#include <stdio.h>

int main(void) {
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("Maximum: %d\n", max);
    printf("The larger number is: %d\n", (a > b) ? a : b);
    return 0;
}
Output
Maximum: 20
The larger number is: 20

Comparison with if-else

The ternary operator can often replace simple if-else statements, making code more concise.

However, it should be used judiciously as complex ternary expressions can reduce readability.

Example
#include <stdio.h>

int main(void) {
    int score = 75;
    char grade;

    if (score >= 60) {
        grade = 'P';
    } else {
        grade = 'F';
    }
    printf("Grade: %c\n", grade);

    grade = (score >= 60) ? 'P' : 'F';
    printf("Grade: %c\n", grade);

    return 0;
}
Output
Grade: P
Grade: P

Evaluation (Only One Branch Runs)

Like if-else, only the selected branch expression is evaluated. This is useful when branches have side effects or are expensive.

Avoid putting complex side effects in both branches; keep ternary focused on producing a value.

Example
#include <stdio.h>

int incr(int *p){ (*p)++; return *p; }

int main(void) {
    int a = 0, b = 0, cond = 1;
    int r = cond ? incr(&a) : incr(&b);
    printf("r=%d a=%d b=%d\n", r, a, b);
    return 0;
}
Output
r=1 a=1 b=0

Nested Ternary Operators

Ternary operators can be nested to handle multiple conditions, but this can quickly become difficult to read.

Nested ternary expressions should be used sparingly and only when they improve clarity.

Example
#include <stdio.h>

int main(void) {
    int num = 0;
    const char *result = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero";
    printf("Number is %s\n", result);
    return 0;
}
Output
Number is Zero

Type Compatibility

The expressions on both sides of the colon should be type-compatible or implicitly convertible to a common type.

The overall type is determined by usual arithmetic conversions or pointer compatibility rules.

⚠️ Warning: Avoid mixing incompatible pointer or struct/union types in the two branches.
Example
#include <stdio.h>

int main(void) {
    int x = 5;
    int result1 = (x > 0) ? 10 : 20;
    double result2 = (x > 0) ? 10 : 20.5; // promoted to double
    printf("result1: %d, result2: %.1f\n", result1, result2);
    return 0;
}
Output
result1: 10, result2: 10.0

Common Use Cases

  • Simple conditional assignments: int max = (a > b) ? a : b;
  • Inline conditionals in function calls: printf("%s\n", (x > 0) ? "Positive" : "Non-positive");
  • Initializing variables based on conditions
  • Returning values conditionally from functions

Best Practices and Pitfalls

  • Use ternary operators for simple, clear conditions only.
  • Avoid deeply nested ternaries; prefer if-else for complex logic.
  • Use parentheses to clarify complex expressions and assignments.
  • Prefer using ?: to compute a value, not to trigger side effects.
  • Ensure both branches produce compatible types to avoid surprises.
Test your knowledge: C Short Hand If (Ternary Operator)
Quiz Configuration
8 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 10 min
C BasicsTopic 28 of 64
←PreviousPrevNextNext→