How does a `union` differ from a `struct` in C?

How does a union differ from a struct in C?

a) Union members share the same memory location
b) Union members have different memory locations
c) Union does not allow multiple data types
d) Union is a function pointer

Answer:

a) Union members share the same memory location

Explanation:

In C, a union is similar to a struct in that it can contain multiple members of different data types. However, unlike a struct, all members of a union share the same memory location, meaning that only one member can hold a value at any given time. The size of a union is determined by the size of its largest member.

This characteristic makes unions useful for saving memory when working with data that can take different forms at different times. However, careful management is required, as changing one member will affect the value of the others.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top