Consider the following C declaration: struct { short s[5]; union { float y;…

2000

Consider the following C declaration:

struct {
   short s[5];
   union {
       float y;
       long z;
   } u;
} t;

Assume that objects of type short, float, and long occupy 2 bytes, 4 bytes, and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is:

  1. A.

    22 bytes

  2. B.

    14 bytes

  3. C.

    18 bytes

  4. D.

    10 bytes

Attempted by 57 students.

Show answer & explanation

Correct answer: C

The structure has two members.

1. s[5] is an array of 5 short values. Since each short occupies 2 bytes, its size is 5 × 2 = 10 bytes.

2. u is a union containing float y and long z. A union occupies the size of its largest member. Here, float requires 4 bytes and long requires 8 bytes, so the union requires 8 bytes.

Ignoring alignment and padding, the total size of t is 10 + 8 = 18 bytes. Therefore, option C is correct.

Explore the full course: Gate Guidance By Sanchit Sir