home *** CD-ROM | disk | FTP | other *** search
- /* U N I O N _ 1
- *
- * Use a union to access the same area of memory
- * as either an integer or float at different times.
- *
- */
-
- #include <stdio.h>
-
- /*
- * Union declaration
- */
- union my_union {
- float float_num;
- int int_num;
- };
-
- int
- main(void)
- {
- /*
- * Declare the union variable union_mem to use for
- * accessing members of the union.
- */
- union my_union union_mem;
-
- /*
- * Save an integer in my_union and print it out.
- */
- union_mem.int_num = 29;
- printf("\nCurrent value of my_union: %d", union_mem.int_num);
-
- /*
- * Now save a float in my_union and print it out.
- */
- union_mem.float_num = 19.58;
- printf("\nCurrent value of my_union: %.2f", union_mem.float_num);
-
- return (0);
- }
-