Saturday, 11 March 2017

extern Keyword

Let us discuss one of the interesting c-keyword extern.Most of us are not aware about extern keyword as we do not use it frequently. To be clear,we are using extern key word unknowingly in our c programs.
For better understanding of extern, it is required to know about declaration and definition.

Declaration vs Definition


Declaration simply declares that variable/function exists some where in the program.
In case of variables,declaration gives information about the datatype of variables.In case of functions declaration gives info about the type of arguments,no.of arguments and return type of function.One of the important thing is to be noted is No memory is allocated for any variable/function in its declaration session.
In addition to declaration,Definition includes allocating memory for variable/function. extern is mainly used to extend the scope of variable/functions.

Use of extern w.r.t C-functions


By default all the functions use extern i.e., both definition/declaration of C functions include extern keyword.

Example:

int functionA(int arg0,float arg1,char arg2)
The compiler treats the above declaration as:
extern int functionA(int arg0,float arg1,char arg2);

We know that all the c-functions are visible throughout the program.This is because of extern keyword, that is responsible for visibility of function throughout the program.

Use of extern w.r.t variables:


To understand the use of extern keyword,let us consider the following examples.

Example 1:


By default all the global variables are extern,if they are not initialized compiler will automatically initialize them with default value
 
int i;   //by default i is an extern variable
int main()
{
printf("%d",i);
return 0;
}
Output:
0
Here we did not use extern keyword,while declaring i.So the compiler will automatically initialize default value to the variable.

Example 2:


When we use extern keyword while declaring variable globally.The behavior is different
 
extern int i;
int main()
{
printf("%d",i);
return 0;
}
Output:
compiler error.
Here, in the above case we declared i as an extern variable so,memory is not allocated to it and we are trying to access it(which doesn't have memory) So the compiler shows an error as unknown symbol i

Example 3:


Generally, memory is not allocated for variables with extern keyword.But there is an exception that extern variables are allocated with memory when they are initialized at the time of declaration.
extern int i=20;
int main()
{
printf("%d",i);
return 0;
}
Output:
20

Example 4:


we need to initialize extern variables outside the function
int main()
{
extern int i=20;
printf("%d",i);
return 0;
}
Output:
compiler error.

This is because extern variables cannot be initialized inside the function

Example 5:


Extern variables can be declared anywhere in the program but they must be initialized outside the block.
int main()
{
extern int i;
printf("%d",i);
return 0;
}
int i=20;
Output:
20

No comments:

Post a Comment