Extern :
All
variables we have seen so far have limited scope (the block in which they are
declared) within the program. However, in some applications it may be useful to
have data which is accessible from within any other file or which remains in
existence for the entire execution of the program.
Suppose
if a variable is declared in file1 and we want to access the variable in
another file2 and file3, then how to access these variables in other files???
Simple, the answer to this is “Extern”.
Extern
keyword is used for the global variables that are defined in one file and can
be used those variables in other files by using extern keyword.
extern int a;
Let us see an example:
file1.c
file2.c
#include<stdio.h> #include<stdio.h>
int main() int a=1;
{ int
main()
extern int a; {return
0;}
printf("%d",a);
return 0;
}
In
this example, the variable ‘a‘ is created and initialized in file2.c and is
used in file1.c as external variable. For information on how to compile a program whose
source code is split among multiple files, you can simply type the command gcc -o a file1.c file2.c
, and run the program
with ./a
.
· When you use extern keyword before the global variable
declaration, the compiler understands you want to access a variable being
defined in another program or file, and hence not to allocate any memory
for this one. Instead, it simply points to the global variable defined in the
other file.
a)
|
#include <stdio.h>
extern int i;
//extern variable
int main()
{
printf("%d",i);
return 0;
}
Output: Compilation error, undefined symbol i.
|
b)
#include<stdio.h>
extern int i;
int main()
{
i = 1;
return 0;
}
This program throws error in
compilation. Because ‘i’ is declared but not defined anywhere. Essentially, the
‘I’ isn’t allocated any memory. In (b), the program is trying to change the value
to 1 of a variable that doesn’t exist at all.
·
If the global variables are not initialized then, they are
initialized to their default values.
Int – 0 , Float – 0.0, Char
- 0 , String – null
#include <stdio.h>
int i; char c; float f; char *str;
int main()
{
printf("%d %d %f %s",c,i,f,str);
return 0;
}
Output: 0 0 0.000000 (null)
· If we declare a variable as extern, it means that the definition for that variable is defined somewhere else in the program or in other file. It doesn’t allocate the memory for these variables. It’s just the declaration. So we can declare it as many times as we can.
#include <stdio.h>
extern int i; //Declaring the variable i.
int i=1; //Initializing the variable.
extern int i; //Again declaring the variable i.
int main()
{
extern int i; //Again declaring the variable i.
printf("%d",i);
return 0;
}
Output: 1
·
A particular extern variable can be declared
many times but we can initialize at only one time. (even outside or inside the
function of same program)
#include <stdio.h>
extern int i; //Declaring the variable
int i=25; //Initializing the variable
int main()
{
printf("%d",i);
return 0;
}
int i=20; //Initializing the variable
Output: Compilation error: Multiple initialization variable i.
·
To define a variable i.e. allocate
the memory for extern variables it is necessary to initialize the variables.
#include <stdio.h>
extern int i=1; //extern variable
int main()
{
printf("%d",i);
return 0;
}
Output: 1
·
We cannot initialize extern variable locally i.e. within any
block either at the time of declaration or separately. We can only initialize
extern variable globally.
a)
#include <stdio.h>
int main()
{
extern int i=1; // Try to initialize extern variable
// locally.
printf("%d",i);
return 0;
}
Output: Compilation error: Cannot initialize extern variable.
b)
#include <stdio.h>
int main()
{
extern int i; // Declaration of extern variable i.
int i=1; // Try to locally initialization of
// extern variable i.
printf("%d",i);
return 0;
}
Output: Compilation error: Multiple declaration of variable i.