Local and global variables

     Local and global variables :

Local variable :

The local variable (or internal variable) is a variable which is declared within a function or within any block of code, between curly braces . The memory is allocated to local variable each time, the control enters the block and released when the control leaves the block. A local variable is accessible only within the block from the point at which it is declared until the end of the block. In other words, no variable can be accessed beyond the block in which it is declared . For example, in the following function, k is a local variable.
  main()
{
  int k;
   .........
   .........
}
It is possible that a block of code can be embedded within another block of code. This is referred to as nested block. In case of nested blocks, a variable declared in the outer block is accessible by the statements of those blocks that are nested within that block, but not vice versa. For example consider the following code,
#include<stdio.h>
#include<conio.h>
main()
{
int x=10;   /*variable x  created*/
clrscr();
printf("x=%d",x);   /*variable x can be accessed*/
{
 int y= 20;   /*variable y declared */
 printf("\nx=%d",x);   /*variable x can be accessed*/
 printf("\ny=%d",y);   /*variable y can be accessed*/
}      /*y dies here*/
printf("\nx= %d",x);   /*variable x can be accessed*/
getch();      /* x dies here */
}

In the above example, variable x is accessible within both the inner and outer blocks as it is declared in the outer block. However, variable y is accessible only within the inner block as it is declared within inner block.

If a variable declared in the inner bock has the same name as that of the outer block then the variable declared in the inner block will be given priority. In other words, the variable declared in the outer block cannot be accessed in the inner block in this case. For instance,
#include<stdio.h>
#include<conio.h>
main()
{
int x=200;
printf("value of x in outer block , x= %d", x);
{
 int x=500;
printf("value of x in inner block, x=%d",x);
}
printf("value of x in outer block, x = %d ",x);
getch();
}
In this example, since the name of the variable x is same in both the inner and outer block so any reference to the inner block x will use declaration of the current and not that of the outer blocks.
   Local variable helps to preserve the integrity of data i.e. data in one function cannot be modified by another function directly , so even if two variables of same name are specified in multiple functions, they are distinct.
NOTE: The formal parameter list specified in the function header accessed only within the function (i.e they are also local variables) even if they occur before the opening brace of the function.

Global variable:

Global variable is a variable that is declared outside all the functions. These variables are known to all the functions in a program i.e. they are accessible to any function from the point where it is declared to the end of the file. For example, consider the following example,
#include<stdio.h>
#include<conio.h>
void func();
int x;  /* global variable*/
main()
{
printf("\nAccessing global variable in main ");
x=100;
printf("\nValue of x=%d", x);
printf("\nCalling function func() now");
func();
printf("\nAfter calling function x=%d",x);
getch();
}
void func()
{
x++;
printf("\nValue of x is increased by 1 ");
}

OUTPUT:Accessing global variable in main
                 Value of x=100
                  calling function func() now 
                 Value of x is increased by 1
                 After calling function x=101

Here, x is a global variable as it is declared before the definition of any function. Hence, variable x is accessible anywhere in the program. Changes made to the variable x from within main(), on execution of the assignment statement,
       x=100;
are reflected in variable x. So when value of variable x is printed , it gives 100. Now when the function main() calls the function func() , the value of x is incremented by 1 and this changes immediately reflected . So when value of x is printed now in main(), it turns out to be 101.    
               Note that if a program contain a local variable with the same name as that of global variable then the local variable takes precedence over the global one.

#include<stdio.h>
#include<conio.h>
int x= 10;    /*global variable */
main()
{
clrscr();
printf("value of global x =%d", x);
x= 125;   /* x reinitialized within block, local variable*/
printf("\nvalue of local x = %d",x);
getch();
}
OUTPUT: value of global x=10
                   value of local x = 125
External variables should be used when multiple functions share a variable or when few functions share a large number of variables. But we strongly recommend that use of global variable should be avoided, instead use the parameter passing technique. The reason is,
  1. If the global variable is assigned an incorrect value, it would be difficult to determine what went wrong and in which part of the program.
  2. If we change a global variable during program modification such as changing the data type, we will need to check every function in the same file so as to see how the change effects it.
  3. Functions that depends on global variables is very difficult to reuse in another program.
                            

                                             THANKS



No comments

Powered by Blogger.