Skip to content
Official PYQ · 2000

GATE CS 2000 Q1.17 — Programming in C

Explain with AI

Consider the following C declaration:

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

Assume that the objects of the type short, float and long occupy 22 bytes, 44 bytes and 88 bytes, respectively. The memory requirement for variable tt, ignoring alignment consideration, is:

Choose one option
View worked solution

Answer: C

To find the memory requirement for the structure variable t, we calculate the sizes of its individual members, ignoring any alignment constraints:

  1. Member x: It is an array of 5 elements of type short.

    Size of x=5×sizeof(short)=5×2 bytes=10 bytes\text{Size of } x = 5 \times \text{sizeof(short)} = 5 \times 2 \text{ bytes} = 10 \text{ bytes}

  2. Member u: It is a union containing a float and a long. Since members of a union share the same memory space, the size of the union is determined by its largest member:

    Size of u=max⁡(sizeof(float),sizeof(long))=max⁡(4 bytes,8 bytes)=8 bytes\text{Size of } u = \max(\text{sizeof(float)}, \text{sizeof(long)}) = \max(4 \text{ bytes}, 8 \text{ bytes}) = 8 \text{ bytes}

Since t is a struct, its total size (ignoring alignment) is the sum of the sizes of its members:

Total size of t=Size of x+Size of u=10 bytes+8 bytes=18 bytes\text{Total size of } t = \text{Size of } x + \text{Size of } u = 10 \text{ bytes} + 8 \text{ bytes} = 18 \text{ bytes}

Therefore, the correct option is C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions