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

  1. /*                        array1.c
  2.  *
  3.  *   Synopsis  - Three arrays are declared and initialized. For
  4.  *               each array, the value in the storage cell with
  5.  *               index 3 is displayed.
  6.  *
  7.  *   Objective - Illustrates declaration, initialization, and
  8.  *               accessing of elements in arrays.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13.  
  14. /* Constant Definitions */
  15. #define NUMCHARS  10
  16. #define NUMFLOATS  8
  17.  
  18. int main( void )
  19. {
  20.      char chararray[NUMCHARS];                                   /* Note 1 */
  21.      int intarray[] = { 2, 1, 3, 5, 4, 8, 3, 7 };                /* Note 2 */
  22.  
  23.      double dblarray[NUMFLOATS] = { 1.2, 3.4, -2.3, 1.4, 4.5 };  /* Note 3 */
  24.  
  25.      int index;
  26.  
  27.                                                                  /* Note 4 */
  28.      for ( index = 0; index < NUMCHARS; index++ )        
  29.           chararray[index] = 127 - index;
  30.  
  31.                                                                  /* Note 5 */
  32.      printf( "chararray occupies %d bytes.\n", sizeof( chararray ) );    
  33.      printf( "intarray occupies %d bytes.\n", sizeof( intarray ) );    
  34.      printf( "dblarray occupies %d bytes.\n", sizeof( dblarray ) );                                        
  35.                                                                  /* Note 6 */
  36.      printf( "The element in chararray with index 3 is '%c'.\n", chararray[3] );    
  37.      printf( "The element in intarray with index 3 is %d.\n", intarray[3] );    
  38.      printf( "The element in dblarray with index 3 is %5.2f.\n", dblarray[3] );    
  39.      return 0;
  40. }
  41.