On a 16-bit Turbo C/DOS platform, consider a struct with three bit-fields declared with base type int. What is sizeof the struct value instance? #include<stdio.h> int main() { struct value { int bit1:1; int bit3:4; int bit4:4; } bit; printf("%d\n", sizeof(bit)); return 0; }

Difficulty: Medium

Correct Answer: 2

Explanation:


Introduction / Context:
The question examines how C packs bit-fields into storage units of the underlying type. With int-based bit-fields on a 16-bit Turbo C model, packing typically occurs within a single 16-bit int until capacity is exceeded.


Given Data / Assumptions:

  • bit1:1, bit3:4, bit4:4 are all declared as int bit-fields.
  • Total requested bits = 1 + 4 + 4 = 9 bits.
  • int size on the assumed 16-bit model is 2 bytes (16 bits).


Concept / Approach:
C compilers pack successive bit-fields of the same base type into the same unit as long as they fit. Here 9 bits fit into one 16-bit int unit; alignment may keep the struct size to that unit boundary when no overflow occurs.


Step-by-Step Solution:

Requested bits = 9 Capacity per int unit = 16 bits All fields fit into one 16-bit unit -> sizeof(bit) = 2 bytes


Verification / Alternative check:
Under Turbo C conventions, consecutive int-based bit-fields pack into the same 16-bit object until overflow. No spillover occurs at 9 bits, so 2 bytes is consistent.


Why Other Options Are Wrong:

  • 1: Too small; cannot hold 9 bits plus alignment.
  • 4: Would correspond to 32-bit packing, not this 16-bit model.
  • 9: Misreads bit count as byte count.


Common Pitfalls:
Assuming modern 4-byte ints or that each bit-field forces a new storage unit; packing rules allow multiple fields per unit when types and order permit.


Final Answer:
2

More Questions from Structures, Unions, Enums

Discussion & Comments

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