10.1 – Scope and Life of variables

by subbu on November 23, 2013

Scope and life of variables in C:

Scope is how far a variable is accessible and life is how much time does a variable is existed in the memory (life of variable). The scope and life of a variable depends on the location where a variable is declared
According to their declaration, variables are classified into 3 categories

  1. Block variables
  2. Internal or Local variables
  3. External or Global variables.

Block variables in C:

Variables declared within a pair of braces { } are called block variables.
A block { } may be an independent or in association with any control structure but, not with a function

{
    /* independent block */
}

if(10<20)
{
     /* block with control structure */
}

Scope of block variables:

Block variables can be accessed within the block in which they are declared, can also be accesses into the inner block which is within the current block but, can’t be accessed outside the block.

Life of block variables:

These variables appear as the control enters into the block and disappears as the control go out of the block. Hence these variables can’t be accessed outside the block.

Example:

/* scope of block variables */
#include<stdio.h>
int main()
{
 {                     /* outer block */
   int x=10;
   {                      /* inner block */
       printf("x=%d",x);
   }
   printf("\nx=%d",x);
 }
 return 0;
}

Output:
x=10
x=10

Example explained:
Scope of block variablesHere x is declared in the outer block, disappears only when control come out the outer block. Hence, it is available for both inner and outer blocks

Example:

/* scope of block variables */
#include<stdio.h>
int main()
{
 if(10<20)
 {
  int x=10;
  printf("x=%d",x);
 }
 printf("\nx=%d",x);    /* can't be accessed */
 return 0;
}

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

Example explained:
Accessing inner block variables in CHere, “if selection statement” has compound statement with a block. The variable x appears as control enter into the block and disappears on exit from the block. Hence variable is not available outside the block.

Overriding block variables:

The main purpose of block variables is to hide outer block variables in the inner block. When inner and outer block variables are with the same name then inner block variable hides (overrides) the outer block variable within the inner block.

Example:

/* hiding block variable */
#include<stdio.h>
int main()
{
 int x=40;
 if(10<20)
 {
   float x=10.25;       /* Hiding outer block variable */
   printf("x=%f",x);
 }
 printf("\nx=%d",x);    /* accessing outer block variable */
 return 0;
}

Output:
x=10.250000
x=40

Example explained:
Overriding block variablesHere there are two variables declared with the same name x both in inner and outer blocks. Generally outer block variable is also available in the inner block but, inner block variable hides the outer block variable within the inner scope because both have the same name.
Inner block variable disappear the movement control come out of the inner block. Hence only outer block variable is available in the outer scope.

Conclusion:

  • Variables declared within a block {} are called block variables
  • Scope of block variables is with in the block
  • Block variables appear as the control enters into the block and disappears as the control go out of the block. So, the life of block variables is with in the block.
  • Default value of block variables is garbage value

Previous post:

Next post: