c / beginner
Snippet
The Ternary Operator
The ternary operator is a shorthand for a simple if-else statement. It evaluates a condition and returns one of two values.
snippet.c
1
2
int age = 20;char* status = (age >= 18) ? "Adult" : "Minor";
Breakdown
1
(age >= 18) ? "Adult" : "Minor"
If age is 18 or more, it returns 'Adult'; otherwise, it returns 'Minor'.