Enumerations in C: determine the printed integral values for three students’ statuses.
#include
int main()
{
enum status { pass, fail, absent };
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d
", stud1, stud2, stud3);
return 0;
}
-
A0 1 2
-
B1 2 3
-
C0 2 1
-
D1 3 2
Answer
Correct Answer: 0 2 1
Explanation
Introduction / Context: This checks understanding of default numbering of enum constants in C and how assigning them to variables results in specific integer outputs with printf.
Given Data / Assumptions:
- enum status defines pass, fail, absent in that order with no explicit values.
- Default enumeration starts at 0 and increments by 1.
- Assignments: stud1 = pass, stud2 = absent, stud3 = fail.
Concept / Approach: With no explicit assignments, pass = 0, fail = 1, absent = 2. The program prints the underlying integer values of the enumerators assigned to stud1, stud2, and stud3.
Step-by-Step Solution:
Derive values: pass = 0; fail = 1; absent = 2.Substitute: stud1 = 0, stud2 = 2, stud3 = 1.printf prints: 0 2 1.Verification / Alternative check: Adding explicit values (e.g., pass = 10) would shift subsequent values. With defaults, the simple 0/1/2 sequence applies.
Why Other Options Are Wrong:
- 0 1 2: Puts absent as 1; incorrect ordering for the given assignments.
- 1 2 3: Assumes start at 1 and increments; not how C enumerations default.
- 1 3 2: Not consistent with any default mapping.
Common Pitfalls: Mixing up the order of printing with the declaration order, or assuming enums start at 1 by default.
Final Answer: 0 2 1