In C, evaluate the values printed for enumerators with explicit and implicit assignments.\n\n#include <stdio.h>\n\nint main()\n{\n enum days { MON = -1, TUE, WED = 6, THU, FRI, SAT };\n printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);\n return 0;\n}\n

Difficulty: Easy

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

More Questions from Structures, Unions, Enums

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion