### Examples of Unions

The following example defines a union data type (not named) and a union variable (named length). The member of length can be a long int , a float, or a double.

    union {
            float meters;
            double centimeters;
            long inches;
          } length;

The following example defines the union type data as containing one member. The member can be named charctr, whole, or real. The second statement defines two data type variables: input and output .

    union data {
                 char charctr;
                 int whole;
                 float real;
               };
    union data input, output;

The following statement assigns a character to input:

    input.charctr = 'h';

The following statement assigns a floating-point number to member output:

    output.real = 9.2;

The following example defines an array of structures named records. Each element of records contains three members: the integer id _num, the integer type_of_input, and the union variable input. input has the union data type defined in the previous example.

    struct {
             int id_num;
             int type_of_input;
             union data input;
           } records[10];

The following statement assigns a character to the structure member input of the first element of records:

    records[0].input.charctr = 'g';