Storage Classes in C
In C language, four types of storage classes are used to store the variables in the RAM or CPU memory register.
- auto storage class
- static storage class
- register storage class
- extern storage class
auto storage class:
It is the default storage class, it is used to declare variables with the keyword auto.
Example :
void main() { void test(void); auto int x; { auto int x=20; printf("Value of x in inner block is %d",x); } test(); printf("Value of x in outer block is %d",x); } void test(void); { auto int x=25; printf("Value of x in functional block is %d", x); }
static storage class:
Static variables are active in the block in which they are declared and they retain the new value, it is also used to declare variables with the keyword static.
Example :
void main(){ void test(void); printf("First functional call"); test(); printf("Second functional call"); test(); } void test() { static int x=10, y=20; x=x+10; y=y+10; printf("Value of x is : %d and y is : %d",x,y); }
register storage class:
Variables declared using this class are stored in the CPU memory register, it is used to declare variables with the keyword register. Only a few variables are used in the program declared by using this class to improve the program execution speed.
Example:
void main() { register int x=10; register char sk='Hello'; printf("Value of x is : %d",x); printf("Value of choice sk is : %c",sk); }
extern storage class:
Extern class is used to consider a local variable in a block as a global variable, it is used to declare variables with the keyword extern.
Example :
void main() { extern int x,y; void test(); x=10; y=20; test(); printf("Value of x is : %d and y is : %d",x,y); } void test() { extern int x,y; printf("Value of x is : %d and y is : %d",x,y); x=x+10; y=y+10; }