C Programming

LOCAL AND GLOBAL VARIABLES

Local
These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.

Global
These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.


DEFINING GLOBAL VARIABLES


	/* Demonstrating Global variables  */
	#include <stdio.h>
	int add_numbers( void );                /* ANSI function prototype */

	/* These are global variables and can be accessed by functions from this point on */
	int  value1, value2, value3;
	
	int add_numbers( void )
	{
		auto int result;
		result = value1 + value2 + value3;
		return result;
	}
	
	main()
	{
		auto int result;
		value1 = 10;
		value2 = 20;
		value3 = 30;		
		result = add_numbers();
		printf("The sum of %d + %d + %d is %d\n",
			value1, value2, value3, final_result);
	}


	Sample Program Output
	The sum of 10 + 20 + 30 is 60

The scope of global variables can be restricted by carefully placing the declaration. They are visible from the declaration until the end of the current source file.


	#include <stdio.h>
	void no_access( void ); /* ANSI function prototype */
	void all_access( void );

	static int n2;		/* n2 is known from this point onwards */

	void no_access( void )
	{
		n1 = 10;        /* illegal, n1 not yet known */
		n2 = 5;         /* valid */
	}

	static int n1;		/* n1 is known from this point onwards */

	void all_access( void )
	{
		n1 = 10;        /* valid */
		n2 = 3;         /* valid */
	}


AUTOMATIC AND STATIC VARIABLES
C programs have a number of segments (or areas) where data is located. These segments are typically,

	_DATA   Static data
	_BSS    Uninitialized static data, zeroed out before call to main()
	_STACK	Automatic data, resides on stack frame, thus local to functions
	_CONST  Constant data, using the ANSI C keyword const

The use of the appropriate keyword allows correct placement of the variable onto the desired data segment.


	/* example program illustrates difference between static and automatic variables */
	#include <stdio.h>
	void demo( void );               /* ANSI function prototypes */
		
	void demo( void )
	{
		auto int avar = 0;
		static int svar = 0;
		
		printf("auto = %d, static = %d\n", avar, svar);
		++avar;
		++svar;
	}
	

	main()
	{
		int i;
		
		while( i < 3 ) {
			demo();
			i++;
		}
	}


	Sample Program Output
	auto = 0, static = 0
	auto = 0, static = 1
	auto = 0, static = 2

Sample program output


Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable. When the function terminates, the variable still exists on the _DATA segment, but cannot be accessed by outside functions.

Automatic variables are the opposite. They are created and re-initialized on each entry to the function. They disappear (are de-allocated) when the function terminates. They are created on the _STACK segment.


©Copyright B Brown. 1984-1999. All rights reserved.