home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / LCNOW2.ZIP / EXAMPLES / UNION_1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-02  |  734 b   |  41 lines

  1. /* U N I O N _ 1
  2.  *
  3.  * Use a union to access the same area of memory
  4.  * as either an integer or float at different times.
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. /*
  11.  * Union declaration
  12.  */
  13. union my_union {
  14.     float float_num;
  15.     int int_num;
  16. };
  17.  
  18. int
  19. main(void)
  20. {
  21.     /*
  22.      * Declare the union variable union_mem to use for
  23.      * accessing members of the union.
  24.      */
  25.     union my_union union_mem;
  26.  
  27.     /*
  28.      * Save an integer in my_union and print it out.
  29.      */
  30.     union_mem.int_num = 29;
  31.     printf("\nCurrent value of my_union: %d", union_mem.int_num);
  32.  
  33.     /*
  34.      * Now save a float in my_union and print it out.
  35.      */
  36.     union_mem.float_num = 19.58;
  37.     printf("\nCurrent value of my_union: %.2f", union_mem.float_num);
  38.  
  39.     return (0);
  40. }
  41.