Consider given C codes :
#include<stdio.h>
extern int i;
int main(){
printf("%d", i);
return 0;
}
It gives Compilation error
. While if I initialize extern int i=10;
then output is 10
.
#include<stdio.h>
extern int i=10; //initialization statement
int main(){
printf("%d", i);
return 0;
}
And also, if I assign int i=10;
then output is 10
.
#include<stdio.h>
extern int i;
int i=10; //assignment
int main(){
printf("%d", i);
return 0;
}
And
#include<stdio.h>
extern int i;
int main(){
int i=10; //assignment
printf("%d", i);
return 0;
}
Variation :
#include<stdio.h>
extern int i;
int i=20;
int main(){
int i=10;
printf("%d", i);
return 0;
}
Since, int i
is local variable, so output is 10
.
Please, can you explain some important point about
extern
storage class in C
I read somewhere, a declaration declares the name and type of variable or function. A definition causes storage to be allocated for the variable or the body of the function to be defined. The same variable or function may have many declaration, but there can be only one defition for that variable or function.
The
extern int i;
declaration just tells the compiler that there somewhere (in this or another source) exists an actual declaration ofi
in global scope. It's not in itself the actual declaration ofi
. Therefore it's inappropriate to assign a value toi
in that declaration (which is why the second try is invalid). Your first attempt is invalid since you don't have an actual declaration ofi
.Your third is OK though as you have an actual declaration of
i
, but you wouldn't have to have theextern int i;
declaration.Your last samples involves
i
in both local and global scope. In such a case the variable in local scope will be used. That's why you don't get 20 printed.when the code references a symbol via the
extern
modifier, it means the symbol is actually defined elsewhere rather than in the current file.An exception, if the
extern
modififier is used for a symbol that is actually defined in the current file, that is OK.The extern storage class specifier is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
Consider
declared outside all the functions of the programme including
main
.This means
i
will haveConsider
declared outside all the functions of the programme including
main
.This means
i
will havei
is only declared, not defined which means space is not allocated.i
is assumed to be defined somewhere else, may be, in an include fileConsider
declared outside all the functions of the programme including
main
.This is the case where you're initializing the
extern
variablei
. In this casei
.10
.extern
is neglected which meansi
is NOTdeclared only
.Note
For
extern int i
it is mandatory that the variable should be defined somewhere else, ie in another source file. If this is not the case, you will a compile-time error.