The greatest mistake you can make in life is to be continually fearing you will make one

Thursday 2 December 2010

Storage Classes in C(Extern)

External Variables (or) Global Variables

  • The scope of a external variables remains from the point of the definition through the remainder of the program.
  • External variables are recognized globally, that can be accessed from any function 
  • Using external variables we can transfer information in to a function without using arguments.
  • The key word for external variable is extern.
External Variable definition:
  • 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
  • Example:
    int x;     //External variable definitions
    fun1(){}
    fun2(){}
  • An external variable definition will automatically allocate the required storage space for the external variables with in the computer memory.
  • The assignment of initial values can be included.
    Example:
    int x=10;     //initializing external variable in definition
    fun1(){}
    fun2(){}
  • The keyword extern is not required in external variable definition.
  • If a function requires an external variable, that has been defined earlier in the program, then the function may access the external variable freely.
    Example:
    #include<stdio.h>
    int x=10;
    fun1
    {
    }
    fun2
    {
        int a,b=10;
        a=b+x; //no need to declare x here
    }
External variable Declaration:
  • If the program is a multi file program  then the external variable must be declared where ever its needed.
  • An external variable declaration begins with the keyword extern.
  • The name and the datatype of the external variable must agree with the corresponding external variable definition that appear outside of the function.
  • Example:
    file1
    #include<stdio.h>
    int x=10; //external var   defined here
    fun1()
    {
    }
    fun2()
    {
    }
    file2
    #include<stdio.h>


    fun3()
    {
        extern int x; //declaration               
    }
    In file 2,x should be of type integer because in file 1, x is defined as integer.
  • The storage space for external variables will not be allocated because the value is already defined.
  • External variable declaration cannot include the assignment of initial value. 

Initialization of External variable:
  • External variables can be assigned initial value during variable definition.
  • The initial values will be assigned only once, at the beginning of the program.
  • The initial values must be expressed as constants not expressions or statements
    Example:
    #include<stdio.h>
    #define z 10
    /*definition of external variable x and Y
    int x=z;
    int y=z+x;       //error
    fun1()
    {
    }
    fun2()
    {
        int a,b=10;
        a=b+x;
        printf("x=%d y=%d\n",x,y);
    }
    int main()
    {
    fun1();
    fun2();
    return;
    }
    while compiling this program It shows an error   "error: initializer element is not constant"
              
  • The external variables will then retain these values, unless they are altered during the execution of the program.
  • If the value of the external variable is changed with in a function, then the changed value will be carried over in to other parts of the program.
    Example
    #include<stdio.h>
    #define z 10
    int x=z;

    fun1()
    {
       x=30;
       printf("In fun1, x=%d \n",x);
    }
    fun2()
    {
        x=20;
        printf("In fun2, x=%d \n",x);
    }
    int main()
    {
        printf("In main, x=%d \n",x);
        fun1();
        fun2();
        return;
    }
    The output of the above program is
    In main, x=10
    In fun1, x=30
    In fun2, x=20
  • If the initial value is not assigned , the variable will automatically be assigned a value of zero.
    Example
    #include<stdio.h>

    int x; //Not initialized

    fun1()
    {
       x=30;
       printf("In fun1, x=%d \n",x);
    }
    fun2()
    {
        x=20;
        printf("In fun2, x=%d \n",x);
    }
    int main()
    {
        printf("In main, x=%d \n",x);
        fun1();
        fun2();
        return;
    }

    The output of the above program is
    In main, x=0
    In fun1, x=30
    In fun2, x=20

1 comment: