Generally we don't specify storage classes while declaring variables in c, but compiler assume a storage class of a variable based on where it is declared. So variables have default storage classes.
But wait what is storage class???
Let me tell you storage class is something by which compiler identifies some physical location within computer where variable will be stored, there two options for storing variable CPU register and Memory.
Storage class determine where to store variable.
There are 4 storage classes defined by c language:
1. Automatic storage class
2. Register storage class
3. Static storage class
4 . External storage class
These classes define where to store variable physically, it's initial value, scope.
But wait what is storage class???
Let me tell you storage class is something by which compiler identifies some physical location within computer where variable will be stored, there two options for storing variable CPU register and Memory.
Storage class determine where to store variable.
There are 4 storage classes defined by c language:
1. Automatic storage class
2. Register storage class
3. Static storage class
4 . External storage class
These classes define where to store variable physically, it's initial value, scope.
External storage class:
Features:-
Storage- memory
Default initial value- Zero
Scope- Global
Life- As long as the program's execution
Global variable use external storage class. They persists during execution of program. Global variable can also shared among files in c. Ex
Output: 9
When a variable is declared as extern it belong to external storage class and compiler do not allocate memory for it, assumes that it exists in some other file. If global variable in above program not declared as extern even now it will work because global variable are by default use external storage class.
Global variable use external storage class. They persists during execution of program. Global variable can also shared among files in c. Ex
- /* File One.c*/
- #include<stdio.h>
- int i=9;
- /*File : two.c*/
- #include<stdio.h>
- #include"prog1.c"
- extern int i;
- void main()
- {
printf("i=%d",i);
- }
Output: 9
When a variable is declared as extern it belong to external storage class and compiler do not allocate memory for it, assumes that it exists in some other file. If global variable in above program not declared as extern even now it will work because global variable are by default use external storage class.
Automatic storage class:
Features:
Storage Memory
Default initial value Garbage value
Scope Local
Life Till the Control remains in the scope
Ex: auto int a;
1.
auto int i=1;
2.
{
3.
auto int i=3;
4.
Printf(“%d”,i);
5.
}
Note - variable i in line 3 is not same as line because both are in different scopes.