Read basic of storage class
Storage Classes mainly classified into four category.
- Automatic Storage Class
- Register Storage Class
- Static Storage Class
- External Storage Class
Automatic storage class : this class provides following functionality to us.
- For using automatic storage class we need to use auto keyword.
- this class stores data in memory.
- default value for variable is garbage
- scope of variable always within block({}) in which it defines.
- Life time until that block({}) execute.
int main()
{
auto int i;
printf("Value of i is ");
printf("%d",i);
}
Output : value of i is 10253
What's this. That's garbage value. If we didn't define value for auto variable then compiler initialize any value for this variable.
Note : auto and normal variables are same.
----------------------------------------------------------------------------------------------------------------------------------
Register storage class : this class provides following functionality to us.
- For using automatic storage class we need to use register keyword.
- this class stores data in cpu register.
- default value for variable is garbage
- scope of variable always within block({}) in which it defines.
- Life time until that block({}) execute.
int main()
{
register int i;
printf("Value of i is ");
printf("%d",i);
}
Output : value of i is 10253
What's this. That's garbage value. If we didn't define value for auto variable then compiler initialize any value for this variable.
Note : Register storage class is used frequently used variables. as accessing variable from cpu register is more fast from memory.
ex. loop counter.
we can't use this class for every variable because cpu registers always limited.
----------------------------------------------------------------------------------------------------------------------------------
Static storage class : this class provides following functionality to us.
- For using automatic storage class we need to use static keyword.
- this class stores data in memory.
- default value for variable is 0.
- value of variable always visible for different function call.
- Life time until execution of program finished.
int main()
{
static int i;
printf("Value of i is ");
printf("%d",i);
}
Output : value of i is 0
What's this. If we didn't define value for auto variable then compiler initialize any value(0) for this variable.
Situation : Function in memory destroyed if their work finished. What if we define static variable into a function then every time new static variable created.
Solution : If we define static variable into a function then compiler reserve separate space for static variable. So if function goes out from memory this variable (static) always in memory because it's memory allocation is different. That's reason if we call same function again and again static variables
doesn't initialized every time.
void demo();
int main()
{
demo();
demo();
}
void demo()
{
static int i;
printf("\n Value of i is ");
printf("%d",i);
i++;
}
Value of i is 0
value of i is 1
----------------------------------------------------------------------------------------------------------------------------------
External storage class : this class provides following functionality to us.
- For using automatic storage class we need to use extern keyword.
- this class stores data in memory.
- default value for variable is 0.
- value of variable always visible into whole program.
- Life time until execution of program finished.
int main()
{
extern int i;
printf("Value of i is ");
printf("%d",i);
}
Output : value of i is 0
What's this. If we didn't define value for auto variable then compiler initialize any value(0) for this variable.
using function
void demo();
int main()
{
extern int i;
demo();
demo();
}
void demo()
{
printf("\n Value of i is ");
printf("%d",i);
i++;
}
Value of i is 0
value of i is 1
No comments:
Post a Comment