The ternary operator lets you choose between two values quickly based on a condition. It makes simple if-else decisions shorter and easier to write.
Ternary operator in C
Introduction
Syntax
C
condition ? value_if_true : value_if_false;
The condition is checked first.
If condition is true, the expression returns value_if_true, otherwise it returns value_if_false.
Examples
a or b to max.C
int a = 10, b = 20; int max = (a > b) ? a : b;
result to "Even" if num is even, otherwise "Odd".C
int num = 5; char* result = (num % 2 == 0) ? "Even" : "Odd";
a equals b, else prints "Not equal".C
printf("%s\n", (a == b) ? "Equal" : "Not equal");
Sample Program
This program checks if age is 18 or more. If yes, it sets can_vote to "Yes", otherwise "No". Then it prints the answer.
C
#include <stdio.h> int main() { int age = 18; char* can_vote = (age >= 18) ? "Yes" : "No"; printf("Can vote? %s\n", can_vote); return 0; }
Important Notes
The ternary operator is a shortcut for simple if-else statements.
Use it only for simple conditions to keep code readable.
Both value_if_true and value_if_false must be expressions that return a value.
Summary
The ternary operator chooses between two values based on a condition.
It is written as condition ? value_if_true : value_if_false.
Use it to make your code shorter and clearer for simple decisions.
Practice
1. What does the ternary operator
condition ? value_if_true : value_if_false do in C?easy
Solution
Step 1: Understand the ternary operator structure
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.Step 2: Determine its purpose
It returns the first value if the condition is true, otherwise the second value.Final Answer:
It chooses one of two values based on the condition's truth. -> Option AQuick Check:
condition ? true_value : false_value = chooses value [OK]
Hint: Remember: condition ? true : false picks one value [OK]
Common Mistakes:
- Thinking it creates loops
- Confusing it with variable declaration
- Assuming it comments code
2. Which of the following is the correct syntax for the ternary operator in C?
easy
Solution
Step 1: Recall ternary operator syntax
The correct syntax is: condition ? value_if_true : value_if_false;Step 2: Check each option
condition ? value_if_true : value_if_false; matches the correct syntax exactly; others have misplaced symbols or punctuation.Final Answer:
condition ? value_if_true : value_if_false; -> Option BQuick Check:
Syntax is condition ? true : false; [OK]
Hint: Remember: question mark before colon in ternary [OK]
Common Mistakes:
- Swapping question mark and colon
- Using comma instead of colon
- Adding extra parentheses incorrectly
3. What is the output of this C code?
int x = 5;
int y = (x > 3) ? 10 : 20;
printf("%d", y);medium
Solution
Step 1: Evaluate the condition (x > 3)
Since x is 5, 5 > 3 is true.Step 2: Apply ternary operator
Because condition is true, y is assigned 10.Final Answer:
10 -> Option AQuick Check:
(5 > 3) ? 10 : 20 = 10 [OK]
Hint: Check if condition is true, then pick first value [OK]
Common Mistakes:
- Choosing the false value by mistake
- Confusing variable values
- Thinking it causes syntax error
4. Identify the error in this C code using the ternary operator:
int a = 10; int b = 20; int max = a > b ? a, b;
medium
Solution
Step 1: Check ternary operator syntax
The ternary operator requires a colon ':' between the two possible values.Step 2: Identify the mistake
The code uses a comma ',' instead of a colon ':', which is incorrect syntax.Final Answer:
Using comma instead of colon in ternary -> Option DQuick Check:
Ternary needs ':' not ',' [OK]
Hint: Ternary uses ':' not ',' between values [OK]
Common Mistakes:
- Using comma instead of colon
- Forgetting colon entirely
- Assuming code is correct
5. Given this code snippet, what will be the value of
result? int a = 0;
int b = 5;
int result = (a != 0) ? (b / a) : (b * 2);
printf("%d", result);hard
Solution
Step 1: Evaluate the condition (a != 0)
a is 0, so (0 != 0) is false.Step 2: Apply ternary operator
Since condition is false, result is assigned (b * 2) = 5 * 2 = 10.Final Answer:
10 -> Option CQuick Check:
Condition false, so result = b*2 = 10 [OK]
Hint: Check condition first to avoid division by zero [OK]
Common Mistakes:
- Assuming division happens causing error
- Ignoring false condition path
- Confusing variable values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
