C - Unions

From EdWiki


  • A union is a variable that may hold (at different times) variables of different types and sizes, with the compiler keeping track of size and alignment requirements.
  • Unions provide a way to manipulate different kinds of data in a single area of storage, without embedding any machine dependent information in the program.
  • The syntax is based on structure
union tagAnyData {
    unsigned char b;
    unsigned int w;
    unsigned long l;
    float f;
    double d;

The variable AnyData will be large enough to hold the largest of the five types.