Variable declaration is not complete until we mention its storage class(there are some default conditions as well). Yeah you got it right ,Variables not only have data types they have storage class too.
Why storage class?
They tell us about
- The scope of the variable.
- Life time of the variable.
- Initial value of the the variable, if left unassigned.
- Automatic storage class
- External storage class
- Static storage class
- Register storage class
Automatic Storage Class
- Scope : Local to the block where it is defined.
- Life Time : Until the control remains in the block.
- Initial value : A garbage value, one cannot predict.
Demonstration of Automatic storage class
#include<stdio.h>
void myFunc();
int main()
{
auto int x = 5;
printf("%d ",x);
myFunc();
return 0;
}
void myFunc()
{
int x;
printf("%d",x);
}
The output is 5 and 'some Garbage value'.
Our compiler in this case treats 'x' differently.This is because the 'x' assigned as 5 is only known inside the main function block, it is undefined for other blocks outside the main function.
The 'x' in the myFunc block exists until the control of the program is inside the function and the x is defined only in that block.
However,It should be noted that if no storage class is mentioned it is a automatic storage class by default.
External Storage Class
- Scope:Global
- Life Time : Until the execution of the program ends.
- Initial Value : Zero (0)
Demonstration of External Storage Class
#include<stdio.h>
void myFunc();
int x;
int main()
{
printf("The value of x is %d\n",x);
int x = 5;
printf("The value of x is %d\n",x);
myFunc();
return 0;
}
void myFunc()
{
printf("The value of x is %d\n",x);
}
The output is
The value of x is 0
The value of x is 5
The value of x is 0
The initial value of x is due to the fact that,global variables left unassigned has a default value of Zero.
After that we assigned a value of 5 to x and got the second output.
However when myFunc function is called the execution context is changed to the block where the value of x is still 0 . i.e. the overwriting of the value of x to 5 is only known inside the main function,so the value of x remains 0 inside myFunc Function.so the third output is 0 .
Static Storage Class
- Scope:Local to block where it is defined.
- Life Time: Until the execution of program ends.
- Initial Value:Zero (0)
Let us look at the program below to understand better.
#include<stdio.h>
void updateGoal();
int main()
{
updateGoal();
updateGoal();
updateGoal();
}
void updateGoal()
{
int goals = 0;
goals++;
printf("%d\n",goals);
}
Here we have a function updateGoal which on call increases the goal by 1 every time.
The output of the above program is:
1
1
1
Every time we are calling the function the value is initialized to 0 and increased by 1.However sometime we might want to preserve the previous value and work on it,this is where static storage class comes to action.
Let us now see the same program but by using declaring the variable as static and see the difference.
#include<stdio.h>
void updateGoal();
int main()
{
updateGoal();
updateGoal();
updateGoal();
}
void updateGoal()
{
static int goals = 0;
goals++;
printf("%d\n",goals);
}
The output is
1
2
3
So,when we declared the variable as static the previous value of goals got preserved.
Register Storage Class
- Scope:Local to the block where it is defined.
- Life Time:Until the control remains in the bock.
- Initial Value:Garbage Value,one cannot predict
- Memory
- CPU Register.
A good example will be declaring the loop counters with register storage class.
#include<stdio.h>
int main()
{
register int i;
for(i = 0 ; i < 5 ; i++)
{
printf("%d" ,i);
}
return 0;
}
The output is
0 1 2 3 4
It should be noted that variable declared else than register storage class are stored in computer Memory.
Only the variable declared with storage class 'register' are stored in CPU registers.
That's It!
Happy Coding
that was such a wonderful article. THANKS 😊
ReplyDeleteYes it is good article.
Delete