On a 16-bit platform, what is sizeof for this enumeration variable?
#include
int main()
{
enum value { VAL1 = 0, VAL2, VAL3, VAL4, VAL5 } var;
printf("%d
", (int)sizeof(var));
return 0;
}
-
A1
-
B2
-
C4
-
D10
Answer
Correct Answer: 2
Explanation
Introduction / Context: This problem checks knowledge of how enumeration types are stored in memory on older 16-bit C implementations and what sizeof yields for an enum object.
Given Data / Assumptions:
- Target platform is 16-bit.
- enum value has five named constants.
- We are printing sizeof(var).
- Assume conventional 16-bit compilers where int is 2 bytes and enum has the same size as int.
Concept / Approach: In C, the size of an enum type is implementation-defined, but historically on 16-bit compilers (e.g., Turbo C), enum typically has the size of int. On such systems, int is 2 bytes, so sizeof(enum variable) is 2.
Step-by-Step Solution:
Identify platform: 16-bit → int size commonly 2 bytes.enum usually stored as int on these compilers.Therefore sizeof(var) = sizeof(int) = 2.Verification / Alternative check: Modern 32-bit or 64-bit compilers often make enum the size of int (commonly 4 bytes). The question explicitly fixes a 16-bit context to remove this ambiguity.
Why Other Options Are Wrong:
- 1: Too small; not typical for an enum mapped to int.
- 4: Common on 32-bit/64-bit, but not the stated 16-bit case.
- 10: Not meaningful for sizeof an enum variable here.
Common Pitfalls: Assuming sizeof(enum) is always 4. It depends on the compiler and target model; always consider platform details given in the stem.
Final Answer: 2