In C, evaluate the values printed for enumerators with explicit and implicit assignments.
#include
int main()
{
enum days { MON = -1, TUE, WED = 6, THU, FRI, SAT };
printf("%d, %d, %d, %d, %d, %d
", MON, TUE, WED, THU, FRI, SAT);
return 0;
}
-
A-1, 0, 1, 2, 3, 4
-
B-1, 2, 6, 3, 4, 5
-
C-1, 0, 6, 2, 3, 4
-
D-1, 0, 6, 7, 8, 9
Answer
Correct Answer: -1, 0, 6, 7, 8, 9
Explanation
Introduction / Context: This item assesses understanding of how C assigns values to enum constants when some are explicitly set and others are left to auto-increment from the previous value.
Given Data / Assumptions:
- MON = -1 explicitly.
- TUE is next after MON, so it becomes 0.
- WED is explicitly 6.
- THU, FRI, SAT follow WED and thus auto-increment.
Concept / Approach: The rule: an unassigned enumerator takes the previous enumerator’s value plus one. When a new explicit value appears (like WED = 6), auto-increment resumes from that value. Therefore THU = 7, FRI = 8, SAT = 9.
Step-by-Step Solution:
MON = -1 (explicit).TUE = -1 + 1 = 0 (implicit).WED = 6 (explicit).THU = 6 + 1 = 7 (implicit).FRI = 8 (implicit); SAT = 9 (implicit).Verification / Alternative check: Printing the constants directly, as in the code, confirms the computed sequence without needing variables or sizeof concerns.
Why Other Options Are Wrong:
- -1, 0, 1, 2, 3, 4: Ignores WED = 6.
- -1, 2, 6, 3, 4, 5: Disrupts order and ignores the increment rule around WED.
- -1, 0, 6, 2, 3, 4: Resets incorrectly after WED.
Common Pitfalls: Assuming auto-increment continues from the earliest explicit value rather than the most recent explicit assignment. Each explicit assignment resets the running value for subsequent implicit ones.
Final Answer: -1, 0, 6, 7, 8, 9