Storage class tells about the storage location ,life time and initial value of a variable/function.
There are four types of storage class in c
1.auto storage class
2.register storage class
3.static storage class
4.extern storage class
let's understand with the help of example
1. auto storage class
example declaration- auto int a ;
or simply - int a ;
or simply - int a ;
A)Storage location -> Storage location of this variable is main memory i.e. if we declare the variable in auto storage mode then that variable will store into RAM
B)Default value -> Default value of this type of variable is GARBAGE , i.e.until we don't assign any value to this variable it will contain GARBAGE value.
C)Scope -> local to the block in which it's defined. i.e. we can use this variable in only the block in which it's defined.
D)LifeTime -> Till the control remains in block .
understand with example !!
main()
{
int a;
printf("%d",a);// garbage value(initial value)
a=10;
printf("%d",a); // 10
{
int b=10;
printf("%d",b);
}
printf("%d",b); // error b is not in its scope
}
if we ignore the third printf
//printf("%d",b); then code will work smoothly and first printf the GARBAGE value because initial value of auto class is GARBAGE ,
in next time 10 will be printed, but in next printf there will be an error
because the variable b is declared in another block and we are printing the value of b in another block.
that's why we say the scope of auto variable is block in which that is defined .
2.Resister storage class
A)Storage location ->Storage location of this variable is CPU register,they are stored in CPU register.but sometimes if the CPU register are not free they will get stored in main memory.
B)Default value -> default value of this type of variable is GARBAGE .initial value to this variable is GARBAGE.
C)Scope -> Scope of this variable is local to the block in which it is defined .
D)LifeTime ->Till control remains within the block
ex- register int a ;
3.Static storage class
A) Storage location -> Storage location of this type of variable is main memory .
B) Default value -> Default value of this variable is ZERO , initially the variable will contain ZERO.
C) scope -> Local to the block in which the variable is defined.
D)Life Time -> Till the value of variable persists between different function calls.
Declaration method -
static int a ;
static int a ;
program to understand the term
int func();
#include<stdio.h> void main() { int i ;
int a=10; for(i=0;i<10;i++) { func(); } } int func() { static int c=10; printf("%d\n",c);c++; return (0); }
Here you can see the value of c is persisting in function calling
4. External storage class
A)Storage -> Storage class of this type of variable is Main memory ,variable of this type is stored in main memory.
B) Default value -> Default value of this type variable is ZERO
C)Scope -> Global ( we can access this variable from any where any block . it's accessible for all block of program.
D)Life Time -> As long as the program execution doesn't comes too an end .
method of declaration
extern int a ;
extern int a ;
remember one thing about extern variable
extern int a ;
// it's a declaration so we have to define it later to use in program
like :
extern int a ;
// it's a declaration so we have to define it later to use in program
like :
extern int a ;
#include<stdio.h>
int main()
{
int a=10;
printf("%d",a);
return 0;
}
|
0 comments: