Open In App
Related Articles

C | Structure & Union | Question 4

Improve Article
Improve
Save Article
Save
Like Article
Like

Consider the following C declaration 
 

C




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


Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000)
 

(A)

22 bytes
 

(B)

14 bytes
 

(C)

18 bytes
 

(D)

10 bytes
 


Answer: (C)

Explanation:

Short array s[5] will take 10 bytes as size of short is 2 bytes. 

When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
 


Quiz of this Question
Please comment below if you find anything wrong in the above post

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Similar Reads