0
1.4kviews
Define union. Compare structure and union. When is union preferred over struct?

Subject : Structured Programming Approach

Title : Arrays, String, Structures and Union

Difficulty : Medium

1 Answer
0
29views

Union: A unionis a special data type available in C that allows storing different data types in the same memory location.

Parameter Structure Union
Keyword The keyword ‘struct’ is used to define a structure. The keyword ‘union’ is used to define a union.
Size When a variable associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. When a variable associated with a union, the compiler allocates the memory by considering the size of the largest memory. Hence, The size of union is equal to the size of largest members.
Memory Each member within a structure is assigned and unique storage area of location. Memory allocated is shared by individual members of union.
Value Altering Altering the value of a member will not affect other members of the structure. Altering the value of any of the member will alter other member values.
Accessing members Individual member can be accessed at a time .Only one member can be accessed at a time.
Initializing Members Several members of a structure can initialize at once. Only the first member of a union can be initialized.
Please log in to add an answer.