Defining a Union Variable

C A union variable definition has the following form:

>>-+-------------------------+--union--union_data_type_name----->
   '-storage_class_specifier-'
 
>--identifier--+-------------------------+---------------------><
               '-=--initialization_value-'
 
 

You must declare the union data type before you can define a union having that type.

Any named member of a union can be initialized, even if it is not the first member. The initializer for an automatic variable of union type can be a constant or non-constant expression. Allowing a nonconstant aggregate initializer is a C99 language feature.

The following example shows how you would initialize the first union member birthday of the union variable people:

union {
      char birthday[9];
      int age;
      float weight;
      } people = {"23/07/57"};

You can define a union data type and a union of that type in the same statement by placing the variable declarator after the data type definition. The storage class specifier for the variable must appear at the beginning of the statement.

Related References

IBM Copyright 2003