Thursday, December 17, 2009

6.3 EXTERNAL (GLOBAL) VARIABLES

External variables,in contrast to automatic variables, are not confined to single functions.Their scope extends from the point the point of definition through the remainder of the program.They are often referred to as global variables.

Since external variables are recognized globally,they can be accessed from any function that falls within their scope.They retain their assigned values within this scope.

When working with external variables, we must distinguish between external variable definitions and external variable declarations.An external variable definition is written in the same manner as an ordinary variable declaration.It must appear outside of ,and usually before,the functions that access the external variables.An external variable definition will automatically allocate the required storage space for the external variables within the computer's memory.The assignment of initial values can be included within an external variable definition if desired.

An external variable declaration must begin with the storage class specifier extern. The name of the external variable and its data type must agree with the corresponding external variable definition that appears outside of the function. Storage space for external variables will not be allocated as a result of an external variable declaration.Moreover, an external variable declaration cannot include the assignment of initial values.These distinctions between an external variable definition and external variable declaration are very important.

Let us write a program that makes use of external variables to represent the total (cumulative) numbers of characters read, and the total number of lines.

/*read several lines of text and determine the average number of characters per line*/

#include <  stdio.h>


int sum=0;               /*total number of characters*/
int lines=0;              /*total number of lines*/


int linecount(void)


main()
{
int n;          /*number of chars in a given line*/
float avg;    /*average number of chars per line*/


printf("Enter the text below\n");


/*read a line of text and update the cumulative counters*/


while (n=linecount() > 0)  {
sum +=n;
++lines;

 }
avg=(float) sum/lines;
printf("\nAverage number of characters per line: %5.2f", avg);

}


/*read a line of text and count the number of characters*/


int linecount(void)
{
char line[80];
int count=0;
while((line[count]=getchar()) != '\n')
++count;
return (count);

}

Notice that sum and lines are external variables that represent the total (cumulative) number of characters read and the total number of lines,respectively.Both of these variables are assigned initial values of zero.These values are successively modified within main,as additional lines of text are read.

Finally, it should  be pointed  out that there are inherent dangers in the use of external variables, since an alteration in the value of an external variable within a function will be carried over into other parts of the program.

No comments:

Post a Comment