Wednesday 12 November 2014

Scope Rules and Extern Variables

Q. What are the scope rules and why we use external variables and how to use them?

Ans : The scope of a variable or a function or a object is the area in the programme where we can use it. Like for example by defining a variable as global ( Outside the main function in c/c++) . We can use that variable in all over the programme.

Scope is generally divided into two broad categories.
1. Global
2. Local

There is one more category that is the external variables through which we can share the variable scope to the not only in the same programme but also in any other file by declaring it as extern variable.

1. Global :  Global scope is as name suggest that we define variable or object globally so any function can use it. Its existence last till the programme last. 

Ex:

# include <stdio.h>

int array[2];   // this is a global variable. this can be used by any of the function
int add(){

    return array[0]+array[1];
}
int main (){

    array[0]=1;
    array[1]=2;
    printf(" %d ", add());
return 0;

}

2. Local : Local scope is the local variable which we declare in different different function for use in only those function. One function can not see the local non static variable of another function.
Ex:
# include <stdio.h>


int add(int array[]){

    return array[0]+array[1];
}
int main (){
 int array[2];   // this is a local variable which can be used only by main function if we want to
                       // add numbers of the array the we have to pass the array as argument to the function
    array[0]=1;
    array[1]=2;
    printf(" %d ", add( array ));  //  passing the array as a argument
return 0;



The difference is that in first scenario every function can use the global variables so we do not have to pass it to every function which needs those variables. But in second scenario we have to  send it as argument to the function which need those variables. 

But in Second scenario variable is lost after the function returns.  Memory is being released. But in the first case all the global variable will last till the programme is not completed . So memory will not be released till the programme ends. 

External Variables:

External variable is a way to share the variables or functions or object between the files. External variable should be declared as extern in the file in which we want to use the variable of the different file. In the first file variables should be global then only other file which has an extern variable can access that variable.


How it happens?

When ever compiler sees an extern variable. It understands that this variable is defined somewhere else , this is only the declaration of it. And then it tries to find out the definition of the variable in the same file in global space and another files which we have included in the file or in the other files which are in the same folder of the file using extern variables.

Ex: in first file:

int array_ext[2];
main(){}

in second file:

extern int array_ext[];

main(){}

I am leaving the not necessary part of the code...
Then in the second file compiler gets to know that array_ext is a external variable so it tries to find its definition in other files. It gets the definition in the file and uses that information. You do not have to declare size of array again in second file. It will automatically detect every thing size ,values if are given.

No comments:

Post a Comment

Related Posts

Related Posts Plugin for WordPress, Blogger...