Conditional Operators

Conditional operators, also known as the ternary operator, provide a shorthand way to perform conditional operations in C.

Syntax

The syntax is:


condition ? expression_if_true : expression_if_false;

Example:


#include <stdio.h>
int main() {
    int a = 5, b = 10;
    int max = (a > b) ? a : b; // Finds the maximum
    printf("Maximum value: %d\n", max);
    return 0;
}

Output

Maximum value: 10