Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the size of the following Union ? Assume that the size of int = 2, size of float = 4, size of char = 1

union tag {
int a;
float b;
char c;
};

  • A.2
  • B.4
  • C.1
  • D.7

Answer: B

All members of a union share the same memory, so its size is that of its largest member. Here the members are an int of 2 bytes, a float of 4 bytes and a char of 1 byte, so the largest is 4 bytes.

A size of 2 or 1 is smaller than the float, and 7 is the sum of the sizes, which is the size of a structure and not of a union, if padding is ignored. Option B is correct. The compiler allocates the space of the largest member to the union, and the members take turns to use it, so only one of them is valid at a time.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.