GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • English
  • Español
  • 中文(简体字)
  • Italiano
  • Português
  • Українська
  • 日本語
  • 한국어
  • Forum
  • Curriculum
  • Donate
  • #c programming

This view

  • Original page
  • New address

GeezerVison is showing www.freecodecamp.org — Ternary Operator in C Explained

Ternary Operator in C Explained

Programmers use the ternary operator for decision making in place of longer if and else conditional statements.

The ternary operator take three arguments:

  1. The first is a comparison argument
  2. The second is the result upon a true comparison
  3. The third is the result upon a false comparison

It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here’s a simple decision-making example using if and else:

int a = 10, b = 20, c;

if (a < b) {
    c = a;
}
else {
    c = b;
}

printf("%d", c);

This example takes more than 10 lines, but that isn’t necessary. You can write the above program in just 3 lines of code using a ternary operator.

Syntax

condition ? value_if_true : value_if_false

The statement evaluates to value_if_true if condition is met, and value_if_false otherwise.

Here’s the above example rewritten to use the ternary operator:

int a = 10, b = 20, c;

c = (a < b) ? a : b;

printf("%d", c);

Output of the example above should be:

10

c is set equal to a, because the condition a < b was true.

Remember that the arguments value_if_true and value_if_false must be of the same type, and they must be simple expressions rather than full statements.

Ternary operators can be nested just like if-else statements. Consider the following code:

int a = 1, b = 2, ans;
if (a == 1) {
    if (b == 2) {
        ans = 3;
    } else {
        ans = 5;
    }
} else {
    ans = 0;
}
printf ("%d\n", ans);

Here's the code above rewritten using a nested ternary operator:

int a = 1, b = 2, ans;
ans = (a == 1 ? (b == 2 ? 3 : 5) : 0);
printf ("%d\n", ans);

The output of both sets of code above should be:

3

Recommended courses from our partners (sidebar)

(opens in a new tab) ADVERTISEMENT
  • On Scrimba Learn JavaScript Per Borgen 9h 21m 9 hours 21 minutes Beginner View course (opens in a new tab)
  • On Scrimba Learn HTML and CSS Per Borgen 5h 40m 5 hours 40 minutes Beginner View course (opens in a new tab)
  • On Udemy Python Mega Course : Go Beginner to Expert in Python3 Funky Programmer 4 out of 5 stars (2,586) from 2,586 ratings 11h 5m 11 hours 5 minutes Beginner Certificate View course (opens in a new tab)
  • On Udemy PYTHON - A to Z Full Course for Beginners Joydip Ghosh and Arjun Reddy 4.1 out of 5 stars (1,376) from 1,376 ratings 1d 48m 1 day 48 minutes Beginner Certificate View course (opens in a new tab)
More Python courses on Class Central →(opens in a new tab)

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Recommended courses from our partners

(opens in a new tab) ADVERTISEMENT
  • On Scrimba Learn JavaScript Per Borgen 9h 21m 9 hours 21 minutes Beginner View course (opens in a new tab)
  • On Scrimba Learn HTML and CSS Per Borgen 5h 40m 5 hours 40 minutes Beginner View course (opens in a new tab)
  • On Udemy Python Mega Course : Go Beginner to Expert in Python3 Funky Programmer 4 out of 5 stars (2,586) from 2,586 ratings 11h 5m 11 hours 5 minutes Beginner Certificate View course (opens in a new tab)
  • On Udemy PYTHON - A to Z Full Course for Beginners Joydip Ghosh and Arjun Reddy 4.1 out of 5 stars (1,376) from 1,376 ratings 1d 48m 1 day 48 minutes Beginner Certificate View course (opens in a new tab)
More Python courses on Class Central →(opens in a new tab)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.