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 >
Wrap
C/C++ Source or Header
|
2005-06-16
|
2KB
|
61 lines
/* global.c
*
* Synopsis - Assigns values to some variables and displays
* those values.
*
* Objective - Illustrates the difference between global and
* local variables
*/
/* Include Files */
#include <stdio.h>
/* Global Variables */
int globalvar; /* Note 1 */
int same_name = 3; /* Note 1 */
/* Function Prototypes */
void sub_fcn( void );
/* PRECONDITION: none.
*
* POSTCONDITION: Assigns values to local and global variables and
* displays the values.
*
*/
int main( void )
{
int localvar; /* Note 2 */
globalvar = 2; /* Note 3 */
localvar = 3; /* Note 4 */
printf( "Starting in main, " );
printf( "globalvar is %d, localvar is %d.\n",
globalvar, localvar );
sub_fcn();
printf( "\nAfter returning to main, " );
/* Note 9 */
printf( "globalvar is %d, localvar is %d,\n",
globalvar, localvar );
printf( "and same_name has value %d.\n", same_name );
return 0;
}
/*******************************sub_fcn()***********************/
void sub_fcn( void )
{
int localvar; /* Note 5 */
int same_name; /* Note 6 */
globalvar = 4; /* Note 7 */
localvar = 5; /* Note 8 */
same_name = 127;
printf( "\nIn sub_fcn, " );
printf( "globalvar is %d, localvar is %d,\n",
globalvar, localvar );
printf( "and same_name has value %d.\n", same_name );
}