home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch7 / bitfld.c next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  58 lines

  1. /*               bitfld.c
  2.  *
  3.  *   Synopsis  - Declares a structure with bit fields, displays
  4.  *               its size in bytes, initializes the structure
  5.  *               and displays the contents of it.
  6.  *   Objective - Provides an example of a structure with bit fields
  7.  */
  8.  
  9. /* Include Files */
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. /* Constant Definitions */
  14. #define SSN_LGTH      10
  15. #define INITIAL_LGTH   4
  16.  
  17. /* Type Descriptions */
  18. struct empl {
  19.      char ssn[SSN_LGTH];
  20.      float rate;
  21.      char initials[INITIAL_LGTH];
  22.      unsigned vested : 1;                       /* Note 1 */
  23.      unsigned years_of_service : 6;             /* Note 2 */
  24. };
  25.  
  26. /* Function Prototypes */
  27. void put_empl( struct empl *emp );
  28. /*   PRECONDITION:  emp contains the address of a struct empl.
  29.  *   POSTCONDITION: Displays the contents of the struct empl passed
  30.  *                  as a parameter.
  31.  */
  32.  
  33. int main( void )
  34. {
  35.      struct empl emp1;
  36.                                                 /* Note 3 */
  37.      printf( "sizeof( struct empl )  %d.\n", sizeof( struct empl ) );
  38.  
  39.      strcpy( emp1.ssn, "567624256" );
  40.      emp1.rate = 7.50;
  41.      strcpy( emp1.initials, "WLF" );
  42.      emp1.vested = 1;                           /* Note 4 */
  43.      emp1.years_of_service = 17;
  44.      put_empl( &emp1 );
  45.      return 0;
  46. }
  47.  
  48. /*******************************put_empl()**********************/
  49.  
  50. void put_empl( struct empl *emp )
  51. {
  52.      printf( "SSN:    %12s\n", emp->ssn );
  53.      printf( "Rate:  $%12.2f\n", emp->rate );
  54.      printf( "Initials%12s\n", emp->initials );
  55.      printf( "vested  %12d\n", emp->vested );   /* Note 5 */
  56.      printf( "years of service %3d\n", emp->years_of_service );
  57. }
  58.