![]() |
VOOZH | about |
A union is a user-defined data type that can hold different data types, similar to a structure.
21 5.20 N
A union is declared similarly to a structure. Provide the name of the union and define its member variables.
The size of the union will always be equal to the size of the largest member of the union. All the less-sized elements can store the data in the same space without any overflow.
Sizeof A: 4 Sizeof B: 40
The above unions' memory can be visualized as shown:
In C, we can define a union inside another union like structure. This is called nested union and is commonly used when you want to efficiently organize and access related data while sharing memory among its members.
21 91
An anonymous union in C is a union that does not have a name. Instead of accessing its members through a named union variable, you can directly access the members of the anonymous union. This is useful when you want to access the union members directly within a specific scope, without needing to declare a union variable.
21 91