10.4 – Life and Storage of global variables

by subbu on November 25, 2013

Variables defined outside all the functions are called external or global variables. In previous session we have discoursed scope of global variables. In this session we will discourse the life and storage of global variables.

Life of external or global variables:

External variables come into existence on initiating the execution of program, stay permanently as long as the program executes. These variables disappear only on completion of program execution.
Unlike local variables, external variables keep their values as long as the program executes.

Example:

/* life of external variables */
#include<stdio.h>
void display();
int main()
{
 display();
 display();
 display();
 return 0;
}
int x=10;       /* external variable */
void display()
{
 x=x+10;
 printf("\n%d",x);
}

Output:
20
30
40

Example explained:
Here in this example, x is an external variable to the function display(), keep its value for the next execution because its  life is permanent until control come out of the total program.
As display() is called for 3 times, for every execution 10 is added to the existed number and the value of x is incremented by 10 for 3 times

Example :

/* Life of external variables */
#include<stdio.h>
void display(int);
int main()
{
 int i;
 for(i=1;i<=5;i++)
   display(i);
 return 0;
}
int a=100;
void display(int x)
{
 a=a+x;
 printf("\t%d",a);
}

Output:
101     103     106     110     115

Example explained:
life of global variablesBecause the external variables are permanent, the value of “a” is preserved for future executions. During the first execution 1 (x) is added to 100 (a), for the second execution 2 is added to 101 and for the third execution 3 is added to 103 etc.

Overriding external variables:

If internal and external variables are with the same name then internal variables hide the external variables with in the local scope.
But external variables are accessed from other functions.

/*Overriding external variables*/
#include<stdio.h>
void display();
int x=45;                      /* external variable definition */
int main()
{
 float x=24.56;              /* automatic variable */
 display();
 printf("\n%f",x);           /* accessing automatic  variable */
 return 0;
}
void display()
{
 printf("\n%d",x);          /* accessing external variable */
}

Output:
45
24.559999

Example explained:
Here x is defined on top of all the functions, accessed from both the function main() and display(). But, there is another variable with the same name x in main() function which is local to main().
Now the global variable x is hided by the local variable x within the main() but, is accessed from display().

Where do the global variables store?

Global variables initialized by the program are store in the area called initialized global segment also called data segment. The data segment has two sections that are read only and read/write. Initialized global variable are stored in the read/write section of dada segment.

Un-initialized global variables are stored in the area called un-initialized global segment or called as BSS (block started by symbol). Operating system initializes the variables stored in BSS with zero before even starting the program execution.

#include<stdio.h>
int a=45;       /* initialized global variable    (in data)*/
int b;          /* un-initialized global variable (in BSS)*/
int main()
{
 int x=25,y=10;  /* local variables of main()  (in Stack)   */
 --------
 return 0;
}

Segments of memory

Even, functions are external:

Because a function can’t be defined in another function, any function must be defined out side the functions.  So, a function is always an external entity.

Conclusion:

  • Variables declared outside the functions are called external or global variables
  • Scope of global variables is limited only to the functions, which are down to their definition, can’t be accessed from the functions above to their definition.
  • External variables come into existence on initiating the program execution, stay permanently as long as the program executes. These variables disappear only on completion of program execution
  • Initialized global variables are stored in Data segment and un-initialized variables are stored in BSS segment
  • The default value of an external variable is zero, even the default elements of an external array are zeros

Previous post:

Next post: