Consider the following union variable declaration in 'C' language: union data { int a; float b; char string[20]; } data1; If 1, 2, and 4 bytes of memory space are allocated to char, int and float variables respectively, then what will be the memory size of the variable data1?
Consider the following union variable declaration in 'C' language: union data { int a; float b; char string[20]; } data1; If 1, 2, and 4 bytes of memory space are allocated to char, int and float variables respectively, then what will be the memory size of the variable data1? Correct Answer 20 bytes
Key Points
Union variable declaration in 'C' language:
union data {
int a;
float b;
char string;
} data1;
- When a variable is associated with a structure, the compiler allocates the memory for each member. The size of the structure is greater than or equal to the sum of the sizes of its members.
-
-
when a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, the size of the union is equal to the size of the largest member.
-
Hence the correct answer is 20 bytes
-
মোঃ আরিফুল ইসলাম
Feb 20, 2025