Consider the following C declaration. Assume that objects of data type short,…
2021
Consider the following C declaration. Assume that objects of data type short, float and long occupy 2 bytes, 4 bytes and 8 bytes respectively. What is the memory requirement for variable t? struct { short a[5]; union { float v; long z; } u; } t;
- A.
22 bytes
- B.
18 bytes
- C.
24 bytes
- D.
10 bytes
Attempted by 507 students.
Show answer & explanation
Correct answer: B
To determine the memory requirement for variable t, we analyze the structure and union components:
1. The array a[5] of type short occupies 5 × 2 = 10 bytes.
2. The union u contains float (4 bytes) and long (8 bytes). The size of a union is determined by its largest member, so u occupies 8 bytes.
3. The total memory requirement is the sum of the array and the union: 10 + 8 = 18 bytes.