Search This Blog

Saturday, May 26, 2012

The Scope and life time of variables in functions


The Scope and life time of variables in functions:
What is scope and lifetime ?
                 The scope of a variable refers that to which different parts of a program have access to the variable in  other words, where the variable is visible. When speaking about scope, the term variable refers to all C data types: simple variables, arrays, structure, pointers, and so forth. It also refers to symbolic constants defined with the const keyword.
The life time of variable refers that how long the variable persists in memory, or when the variable's storage is allocated and de-allocated. Depending upon the scope and lifetime of variable, C has its  four storage classes for variables used in any functions.
  1. Automatic variables
  2. External variables
  3. Static variables
  4. Register variables
The variables may broadly categorized , depending upon the place of their declaration as: internal(local) or External(global).
·         The internal(local) variables are those which are declared inside the function.
·         The external(global) variables are those which are declared outside the  function.
Automatic variables:
            An automatic variable is defined inside the main() or function program. It is created when the function is called and get distroyed when the function segment is exited. Thus the visibility and lifetime of automatic variable is limited to the function execution. The key word ‘auto’ is used to declare the automatic variable. It is declared as:
auto  data_type  variable_name;
            Generally the keyword auto maybe dropped out. The variable declared inside function are default automatic if not any other keywords are used. These are also called local variables.
e.g.
            main()
           {
               int num;        /* num is automatic variable */
                ……..
            }
External variable:
            The external variables are declared outside the main or function block. These variables are set up memory immediately on the declaration and remain active for the entire period of program execution. Its visibility(scope) is that of the source file. It they are not initialized then they are automatically initialized to zero values. External variables are accessed by all the functions in the program. These variables are also known as global variables.
            The keyword ‘extern’ is used  to declare these variables. But it is optional. We can use extern to avoid confusion.
  extern  data_type  variable_name;
e.g.   int  sum,difference;        /* external variable */
         char name[10];
         main()
  {
     ……..
  }

Static variable:
            Static variables are generally defined inside main() or function block with keyword ‘static’ prefixed to it. They are sometimes called static auto variables as they have visibility of local (or auto) variables  and lifetime of external ( or global ) variables. Static variables are initialized once during the first function call.  These variables are declared as
static data_type  variable_name;
e.g. static int counter;

Register variables:
            If we want to tell the compiler that a variable should be kept in one of the machine’s register instead of keeping in the memory( where normal variables are stored), variables should be declared as register. The register access is much faster than memory. So keeping frequently access variables in register makes the execution of program faster. The declaraction is done as:
 register  data_type variable_name;
e.g.  register int count;
Only a few variables can be stored in register. However C compiler converts register variables into non register variables once the limit is reached.

No comments:

Post a Comment