External storage classes
1.) Extern keyword will be use to represent external storage class.
2.) Under external storage class default value taken zero if there is no
initialization with variable declaration in program.
3.) In this storage class scope of a variable is up to end of program.It means value of variable under external storage class can be access any where in program.Life of variable up to end of program.
4.) Value of variable will be store under RAM.
Variable declaration out side of main function,function and block comes under external storage class.
Let us understand with examples-
1.) #include<stdio.h>
int x=1;
void fun();
int main()
{
printf("%d",x);
fun();
printf("%d",x);
return 0;
}
void fun()
{
x++;
printf("%d",x);
}
Output : 122
2.) #include<stdio.h>
int x=1;
void fun();
int main()
{
extern int x;
printf("%d",x);
fun();
printf("%d",x);
return 0;
}
void fun()
{
x++;
printf("%d",x);
}
Output : 122
As you are seeing that the output of both examples are the same, then you must be confused that the Extern keyword is use less. You may be thinking the same, but it is not so.Just think that the int x=1; would not have been done before the function in declaration then what happen.
Note
As we know that before using of variable we need to declare it.When we do not declare the variable first before the main function,the compiler will give error. when we do not declare variable first before main function but it is declared further in the program and do not told the compiler then compiler give error.
Let us see example and understand --
1.) #include<stdio.h>
void fun();
int main()
{
printf("%d",x);
fun();
printf("%d",x);
return 0;
}
int x=1;
void fun()
{
x++;
printf("%d",x);
}
Output : error will come so there is no output.
2.) #include<stdio.h>
void fun();
int main()
{
extern int x;
printf("%d",x);
fun();
printf("%d",x);
return 0;
}
int x=1;
void fun()
{
x++;
printf("%d",x);
}
Output : 122
Comes error after running the program in the first example. The first example has a variable declaration, but as we know it is a rule that the declaration is necessary before its use any where in program.
Here the compiler is confused that the declaration is not given in the program at first.In such a situation, it is necessary to tell the compiler that the declaration is ahead in the program, as you would see in the second example that we have told it before using the variable, And the compiler is not confused then it gives the output 122 .So you must have seen that we need the extern keyword when we have to tell the compiler that the declaration of the variable have been written further in the program.
Note
1. ) Extern x=1 this statement is not a variable declaration.It is a method to tell the compiler.
2. ) Keep in mind extern int x; is correct syntax and extern int x=1; not a correct statement.See example -
#include<stdio.h>
void fun();
int main()
{
extern int x=1; // this is not correct syntax so error will come
printf("%d",x);
fun();
printf("%d",x);
return 0;
}
int x=1;
void fun()
{
x++;
printf("%d",x);
}