C Ternary Operator
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Nesting several
?:operators so deeply that the code becomes unreadable compared with an if-else chain. - Forgetting parentheses in a larger expression such as
a + b > c ? x : y, where precedence gives an unexpected grouping. - Using branches that produce different types, such as a string and an int, which does not compile or gives warnings.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: