In the following C code that declares an enum and uses a switch statement, what output will be produced when the program is executed? #include <stdio.h> enum actor { SeanPenn = 5, AlPacino = -2, GaryOldman, EdNorton }; int main(void) { enum actor a = 0; switch (a) { case SeanPenn: printf("Kevin Spacey"); break; case AlPacino: printf("Paul Giamatti"); break; case GaryOldman: printf("Donald Shuterland"); break; case EdNorton: printf("Johnny Depp"); break; } return 0; }

Difficulty: Medium

Correct Answer: Johnny Depp

Explanation:


Introduction / Context:
This question tests knowledge of how enumeration constants are assigned values in C when some constants have explicit initialisers and others do not. It also checks whether you can follow control flow through a switch statement that compares an enum variable with those constants. The subtle part is understanding what numeric value each enum constant receives and therefore which case label matches the value assigned to a.


Given Data / Assumptions:

  • The enum actor explicitly sets SeanPenn to 5 and AlPacino to -2.
  • GaryOldman and EdNorton do not have explicit values.
  • In C, uninitialised enum constants take the previous value plus one.
  • The variable a is initialised with 0 and used as the switch expression.


Concept / Approach:
When an enum constant has no explicit initialiser, C uses the rule that it equals the previous constant plus one. For the first constant in the list, the default starting value is zero if there is no explicit initialiser. In this case, SeanPenn is 5 by definition. AlPacino is explicitly -2. Next, GaryOldman inherits the value of AlPacino plus one, which is -1. Finally, EdNorton is GaryOldman plus one, so it becomes 0. When the program sets enum actor a = 0; it is giving a the same numeric value as EdNorton, so the switch will match that case label.


Step-by-Step Solution:
Step 1: Assign SeanPenn the value 5 as given by its explicit initialiser. Step 2: Assign AlPacino the value -2 as given by its explicit initialiser. Step 3: Apply the default rule for GaryOldman: its value is AlPacino + 1, which is -1. Step 4: Apply the default rule for EdNorton: its value is GaryOldman + 1, which is 0. Step 5: Note that the variable a is initialised to 0, which equals the value of EdNorton, so the switch executes the case EdNorton branch and prints "Johnny Depp".


Verification / Alternative check:
You can verify the numeric values by writing a small program that prints (int)SeanPenn, (int)AlPacino and so on. Doing so will show 5, -2, -1 and 0 respectively on typical implementations. Since a is set to 0, comparing it to each case label will succeed only for EdNorton. There is no default case, but it is not needed because a always matches one of the enumerators in this example.


Why Other Options Are Wrong:
Option A would be correct only if a had the value 5, matching SeanPenn, which it does not. Option B would require a to be -2, matching AlPacino, which is also not the case. Option C would require a to be -1, corresponding to GaryOldman. In all of these cases, a is explicitly initialised to 0, so none of those case labels applies and their associated strings are never printed.


Common Pitfalls:
Students sometimes assume that enumerators always start at zero and increase by one, ignoring explicit negative or positive initialisers in the middle of the list. Another common error is to think that a variable of enum type can hold only one of the named constants, forgetting that it is essentially an int and can store any value in the implementation range, including ones that do not correspond to a defined enumerator. Here, careful reading of the initialisers avoids such confusion.


Final Answer:
The program prints Johnny Depp because the enumerator EdNorton has the value 0, which matches the value assigned to a in the switch statement.

More Questions from Programming

Discussion & Comments

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