Unions In C

Unions are derived data types, they are like structures, contain members whose individual data type may differ from one another. There is major distinction between them in terms of storage. The members within a union all shared the same storage area within the computer’s memory, where each member within a structure is assigned its own unique storage area. Thus, unions are used to conserve memory. In structures, each member has its own storage location, whereas all the members of a union use the same location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. The syntax of union is as follows:

union tagname
{
    first member declaration;
    .  .  .  .  .
    .  .  .  .  .
    last member declaration;
};

Usage
  • • The union aids in the production of machine – independent code because the compiler keeps track of the actual sizes of the variable which make up the union.
  • • Unions are frequently used when type conversions are needed because the data held in a union can be referred to in different ways.


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext