Skip to content
StudyFame

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

  1. (a > b): is 18 > 25? No, it is false.
  2. Because the condition is false, the value after : is chosen: b (25).
  3. 25 is stored in max and printed.

How the Ternary Operator Works

  1. C checks the condition first.
  2. If it is true (non-zero), it evaluates only the first value.
  3. If it is false (0), it evaluates only the second value.
  4. The chosen value becomes the result of the whole expression.
  5. It has very low precedence and works right to left, so a ? b : c ? d : e means a ? 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

MistakeWhat happensFix
a > b ? a (no : part)Syntax errorBoth parts are required
Swapping the values: a > b ? b : a for maxGives the minimumTrue value comes right after ?
Deeply nested ternaries on one lineHard to read and debugUse if-else for complex logic
Using it just to call functions: x ? f() : g();Works but confusingUse if-else for actions

Ternary vs if-else

Ternary operatorif-else
An expression: returns a valueA statement: does actions
One lineSeveral lines
Best for choosing between two valuesBest for many statements or complex logic
Can be used inside printf or an assignmentCannot be used inside an expression

Ternary Operator Practice Questions

  1. What is the ternary operator? Why is it called ternary?
  2. Write the syntax of the conditional operator.
  3. Write one statement to find the smaller of two numbers x and y.
  4. What is the output of printf("%s", 7 % 2 ? "odd" : "even");?
  5. Write two differences between the ternary operator and if-else.
Show answers
  1. A conditional operator ? : that returns one of two values based on a condition. It is called ternary because it has three operands.
  2. condition ? expression1 : expression2;
  3. min = (x < y) ? x : y;
  4. odd (7 % 2 is 1, which is true).
  5. 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)

  1. How many operands does the conditional operator take?

    A) 1   B) 2   C) 3   D) 4

  2. Output of printf("%d", 5 > 3 ? 10 : 20);

    A) 5   B) 10   C) 20   D) 1

  3. Which finds the maximum of a and b?

    A) a < b ? a : b   B) a > b ? a : b   C) a > b : a ? b   D) a ? b : a > b

  4. The ternary operator's associativity is:

    A) Left to right   B) Right to left   C) None   D) Depends on compiler

  5. Output of printf("%d", 0 ? 1 : 2);

    A) 0   B) 1   C) 2   D) Error

Show answers
  1. C: 3.
  2. B: 10.
  3. B: a > b ? a : b
  4. B: right to left.
  5. 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? :
Syntaxcondition ? true_value : false_value
Operands3 (only ternary operator)
Max of a, ba > b ? a : b
AssociativityRight to left
ReturnsA 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.