← Back to C Course | Chapter 3: Operators | Lesson 8 of 9

C Ternary Operator

The ternary operator is a one-line way to pick between two answers based on a question, like 'if it rains take an umbrella, otherwise sunglasses'. It is a short if-else.
Syntax
c
variable = (condition) ? value_if_true : value_if_false;

What is the Ternary Operator?

The ternary operator condenses a simple if-else into one expression using the syntax condition ? valueIfTrue : valueIfFalse, which is especially handy when you just need to pick between two values rather than run different blocks of statements.

Example: What is the Ternary Operator?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	char *status = (age >= 18) ? "Adult" : "Minor";
	// Print formatted text to the screen
	printf("%s", status);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Finding the Maximum

Finding the larger of two numbers can be written in one line as max = (a > b) ? a : b;, avoiding a full four-line if-else block for what's fundamentally a simple choice between two values.

Example: Finding the Maximum

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 7, b = 12;
	// Declare max and set it to (a > b) ? a : b
	int max = (a > b) ? a : b;
	// Print formatted text to the screen
	printf("%d", max);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pass or Fail Check

For binary outcomes like pass/fail, the ternary operator lets you compute a result directly: result = (score >= 50) ? "Pass" : "Fail";, keeping the logic compact and readable in a single assignment.

Example: Pass or Fail Check

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare score and set it to 65
	int score = 65;
	char *result = (score >= 50) ? "Pass" : "Fail";
	// Print formatted text to the screen
	printf("%s", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Even or Odd

Combining n % 2 == 0 with the ternary operator lets you classify a number as even or odd in a single expression, such as label = (n % 2 == 0) ? "Even" : "Odd";, without a separate if statement.

Example: Even or Odd

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 7
	int n = 7;
	char *label = (n % 2 == 0) ? "Even" : "Odd";
	// Print formatted text to the screen
	printf("%s", label);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Ternary

You can nest ternary operators to handle more than two outcomes, but each level of nesting makes the expression harder to read at a glance -- past two conditions, a regular if-else ladder is usually clearer.

Example: Nested Ternary

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare score and set it to 75
	int score = 75;
	char *grade = (score >= 90) ? "A" : (score >= 70) ? "B" : "C";
	// Print formatted text to the screen
	printf("%s", grade);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Nesting several ?: operators so deeply that the code becomes unreadable compared with an if-else chain.
  2. Forgetting parentheses in a larger expression such as a + b > c ? x : y, where precedence gives an unexpected grouping.
  3. Using branches that produce different types, such as a string and an int, which does not compile or gives warnings.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.