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

  1. /*            global.c
  2.  *
  3.  *   Synopsis  - Assigns values to some variables and displays 
  4.  *               those values.
  5.  *
  6.  *   Objective - Illustrates the difference between global and 
  7.  *               local variables
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Global Variables */
  14. int globalvar;                                         /* Note 1 */
  15. int same_name = 3;                                     /* Note 1 */
  16.  
  17. /* Function Prototypes */
  18. void sub_fcn( void );
  19. /* PRECONDITION:  none.
  20.  *
  21.  * POSTCONDITION: Assigns values to local and global variables and 
  22.  *                displays the values.
  23.  *
  24.  */
  25.  
  26. int main( void )
  27. {
  28.      int localvar;                                      /* Note 2 */
  29.  
  30.      globalvar = 2;                                     /* Note 3 */
  31.      localvar = 3;                                      /* Note 4 */
  32.      printf( "Starting in main, " );
  33.      printf( "globalvar is %d, localvar is %d.\n", 
  34.                                              globalvar, localvar );
  35.  
  36.      sub_fcn();
  37.  
  38.      printf( "\nAfter returning to main, " );
  39.                                                         /* Note 9 */
  40.      printf( "globalvar is %d, localvar is %d,\n",
  41.                                              globalvar, localvar );
  42.      printf( "and same_name has value %d.\n", same_name );
  43.      return 0;
  44. }
  45.  
  46. /*******************************sub_fcn()***********************/
  47.  
  48. void sub_fcn( void )
  49. {
  50.      int localvar;                                      /* Note 5 */
  51.      int same_name;                                     /* Note 6 */
  52.  
  53.      globalvar = 4;                                     /* Note 7 */
  54.      localvar = 5;                                      /* Note 8 */
  55.      same_name = 127;
  56.      printf( "\nIn sub_fcn, " );
  57.      printf( "globalvar is %d, localvar is %d,\n",
  58.                                                globalvar, localvar );
  59.      printf( "and same_name has value %d.\n", same_name );
  60. }
  61.