10.3 – External or global variables – Scope

by subbu on November 25, 2013

Disadvantages of automatic variables (Local variables):

  • Because automatic variables come and go with the function execution, they can’t keep their values for the next execution; we need to initialize these variables for every execution otherwise garbage value is stored.
  • If multiple functions want to work on the same data, then data must be sent to functions as arguments separately.

These issues can be easily addressed through global variables

External or global variables:

Variables declared outside the functions are called global variables. These variables are available along down to the program from their declaration.

Scope of external variables:

Global variables are visible only to the functions, which are down to their definition, can’t be accessed from the functions above to their definition.

Example:

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

Output:
x=10
x=60

Example explained:
Scope of global variables in CHere in this example, “x” is declared on top of all the functions. It is global to both the functions main() and display(). The change in its value (x=x+50) in the function display() reflected the change in main() because x is a common variable to both the functions.

Example:

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

Output:
Error: undefined symbol “x” in function main().

Example explained:
Scope of global variablesHere x is an external variable because declared outside both the function main() and display().
Because it is declared below the main() and above the display(), It is global to display() but, not to the main(). Hence x can’t be accessed from the main(), which results error.

Default value of external variable:

External variables are initialized with zero if not assigned with any value by the program

Example:

#include<stdio.h>
int x;               /* external variable */
int main()
{
 printf("\n%f",x);
 return 0;
}

Output:
0

Elements of an external array are initialized with zeros if not assigned with any values

Example:

#include<stdio.h>
int x[5];  /* defining an external array */
int main()
{
 int i;
 for(i=0;i<5;i++)
   printf("%5d",x[i]);
 return 0;
}

Output:
0  0  0  0  0

External variable initializer must be a constant:

external variables come into existence even before starting the execution of program. Hence, an external variable can’t be initialized with a variable.

#include<stdio.h>
int x=10;  /* external variable initializing with constant */
int y=x;  /* external variable initializing with a non-constant */
int main()
{
 printf("x=%d",x);
 printf("\ny=%d",y);
 return 0;
}

Output:
error: initializer element is not constant

External data can be shared by multiple functions:

Because external variables are globally available, data can be commonly shared by multiple functions. It would be more convenient rather sending arguments and returning values. The following programs demonstrate ease in using global variables comparatively with local variables.

Specification: Access n elements into an array and print the minimum and maximum elements of array using two functions minimum () and maximum().

#include<stdio.h>
int minimum(int[],int);
int maximum(int[],int);
int main()
{
 int a[50],n,i;
 printf("How many elements?");
 scanf("%d",&n);
 printf("Enter the elements..\n");
 for(i=0;i<n;i++)
   scanf("%d",&a[i]);
 printf("Minimum element %d",minimum(a,n));  /* sending arguments */
 printf("\nMaximum element %d",maximum(a,n));/* sending arguments */
 return 0;
}
int minimum(int p[],int n)
{
 int i,min;
 for(min=p[0],i=0;i<n;i++)
   if(p[i]<min)
     min=p[i];
 return min;                          /* returning value */
}
int maximum(int p[],int n)
{
 int i,max;
 for(max=p[0],i=0;i<n;i++)
   if(p[i]>max)
     max=p[i];
 return max;                          /* returning value  */
}

Execution:
How many elements?5
Enter the elements..
45 67 12 6 20
Minimum element 6
Maximum element 67

Same program with global variables

#include<stdio.h>
int a[50],n,i,min,max;              /* global declarations */
void minimum();
void maximum();
int main()
{
 printf("How many elements?");
 scanf("%d",&n);
 printf("Enter the elements..\n");
 for(i=0;i<n;i++)
   scanf("%d",&a[i]);
 minimum(),maximum();               /* calling functions */
 printf("Minimum element %d",min);
 printf("\nMaximum element %d",max);
 return 0;
}
void minimum()
{
 for(min=a[0],i=0;i<n;i++)
  if(a[i]<min)
    min=a[i];                   /* assigning minimum element */
}
void maximum()
{
 for(max=a[0],i=0;i<n;i++)
   if(a[i]>max)
      max=a[i];                 /* assigning maximum element */
}

Execution:
How many elements?5
Enter the elements..
45 67 12 6 20
Minimum element 6
Maximum element 67

Examples explained:
In the first program, array a[] and variables i, n are local to main(), variables min and max are local to the sub functions minimum() and maximum(). Here function to function communication is set through the arguments and returning values.
In the second program, array a[] and variables i, n, min and max are declared globally; hence, shared by all the functions without passing arguments and returning values.

Conclusion:
Global variables are generally used where data need to be commonly shared by multiple functions instead sending arguments and returning values.

Problems with the global variables are

  • These variables are always there even we don’t want. So, need more memory to run an application
  • As these variables may be accessed at different places, it is difficult to trace out and modify the program.

The life of external variables will be discoursed in the next session

Previous post:

Next post: