In C, what output is produced by the following enumeration and assignments (default enum starting at 0)?
#include
int main()
{
enum status { pass, fail, atkt };
enum status stud1, stud2, stud3;
stud1 = pass; // 0
stud2 = atkt; // 2
stud3 = fail; // 1
printf("%d, %d, %d
", stud1, stud2, stud3);
return 0;
}
-
A0, 1, 2
-
B1, 2, 3
-
C0, 2, 1
-
D1, 3, 2
-
E2, 0, 1
Answer
Correct Answer: 0, 2, 1
Explanation
Introduction / Context:C enumerations by default assign integer values starting at 0 and increment by 1 unless explicitly set. Understanding this default behavior helps in reading printed enum values and interfacing with APIs.
Given Data / Assumptions:
- enum status { pass, fail, atkt }; with no explicit initializers.
- Assignments: stud1 = pass; stud2 = atkt; stud3 = fail;
- Printing via printf("%d, %d, %d", ...)
Concept / Approach:By default: pass = 0, fail = 1, atkt = 2. The program prints the integer values corresponding to the assigned enumerators.
Step-by-Step Solution:Assign defaults: pass→0, fail→1, atkt→2.Map variables: stud1=0, stud2=2, stud3=1.Print order is stud1, stud2, stud3 → “0, 2, 1”.
Verification / Alternative check:Adding explicit initializers (e.g., pass=10) would change the printed numbers accordingly; here no such initialization exists.
Why Other Options Are Wrong:A/D/E list other permutations not matching the actual assignments. B starts at 1 which is not the default.
Common Pitfalls:Assuming enums start at 1; forgetting the order of printing vs declaration order.
Final Answer:0, 2, 1