32 views

Please explain the solution along with the concept.

| 32 views

The memory allocated to a union will be equal to the size of the largest data type it contains. In this case, that data type is int.

For this question - the sizeof(abc) will be the same as sizeof(int). And that could be 2 or 4 (or otherwise) depending on the platform of execution. Such questions are usually ambiguous because the size of any data type is ultimately dependent on the underlying platform.

by (1.9k points)
0

The memory allocated to a union will be equal to the size of the largest data type it contains. In this case, that data type is int.

I am not sure about whether it will take the size of the largest data type or collectively largest space taken by all the variable of that particular data type (i.e for all the char variable collectively)?

0
You're thinking about a structure. Read up on the behavior of a union. And also simply execute the given program to make sure..
0
let me clear the things up

structure will take the space collectively by int and char variables both i.e. sizeof(int)+sizeof(char)

union will take the space taken by the largest data type i.e. sizeof(int)

right?

this might be silly but please make it clear -

I thought union will take the size equal to the sum of the size of all characters means there are 8 characters (a,b,c,d,e,f,g,h) and each character will take 1B so total bytes allocated will be 8B , this is not true, right? all charecters will reside in the 1B space.