Unions and Enumerations in C programming


An union is a derived data type—like a structure—with members that share the same storage space. The members of a union can be of any data type.

A union definition has the same format as a structure definition.

    union number {   
    int x;  // int member x 
    double y;// double member y
    }; // end union number

The union definition is normally placed in a header and included in all source files that use the union type

The operations that can be performed on a union are:

  • assigning a union to another union of the same type.
  • taking the address (&) of a union variable.
  • and accessing union members using the structure member operator and the structure pointer operator.

Demonstrating Unions

The program uses the variable value of type union number to display the value stored in the union as both an int and a double.

The program output is implementation dependent. The program output shows that the internal representation of a double value can be quite different from the representation of int.

    // Displaying the value of a union in both member data types
    #include <stdio.h>
    // number union definition
    union number {               
    int x;                    
    double y;                 
    }; // end union number    
    int main( void ) { 
    union number value; // define union variable
    value.x = 100; // put an integer into the union
    printf( "%s\n%s\n%s\n  %d\n\n%s\n  %f\n\n\n",
      "Put 100 in the integer member", 
      "and print both members.",
      "int:", value.x, 
      "double:", value.y );
    value.y = 100.0; // put a double into the same union
    printf( "%s\n%s\n%s\n  %d\n\n%s\n  %f\n",
      "Put 100.0 in the floating member",
      "and print both members.",
      "int:", value.x, 
      "double:", value.y );
    } // end main

    Put 100 in the integer member and print both members.
    int: 100
    double: -92559592117433136000000000000000000000000000000000000
    Put 100.0 in the floating member and print both members.
    int: 0
    double: 100.000000

Enumeration Constants

An enumeration ( enum ), is a set of integer enumeration constants represented by identifiers. Values in an enum start with 0, unless specified otherwise, and are incremented by 1.

    enum months { 
    //Initialize JAN=1 to number the months 1 to 12   
    JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG,    
    SEP, OCT, NOV, DEC }; // end enum months

The identifiers in an enumeration must be unique. Multiple members of an enumeration can have the same constant value.

Demonstrating Enums

The code below show this:

    // Using an enumeration
    #include <stdio.h>
    // enumeration constants represent months of the year            
    enum months {                                                    
    }; // end enum months                                            
    int main( void ) { 
    enum months month; // can contain any of the 12 months   
    // initialize array of pointers
    const char *monthName[] = { "", "January", "February", "March", 
      "April", "May", "June", "July", "August", "September", "October",
      "November", "December" };
    // loop through months
    for ( month = JAN; month <= DEC; ++month ) {
      printf( "%2d%11s\n", month, monthName[ month ] );
    } // end for
    } // end main

    Program output:
    1       January
    2       February
    3       March
    4       April
    5       May
    6       June
    7       July
    8       August
    9       September
    10      October
    11      November
    12      December

Ads Right