Storage class in c help us to understand in which area of memory a particular variable is stored . Also defining scope and visibility of variables in the program. In c there are 4 important storage classes which are follows
- auto
- static
- register
- extern
What is scope and visibility of a variable in c ?
Scope of a variable is the part of program within which the variable can be used. That is check a variable is exist or not , if the variable is exist means that variable does not destroy from memory. The part of program within which the variable can be used is known as its scope. Visibility is the accessibility that is up to which part of program we can access the variable. Scope and visibility of each storage class is different. In the below figure scope of the static variable a is the outer red box and visbility of a is yellow colour part.
auto storage class
An auto storage class declares automatic variables in c language . Automatic variables has lifetime locally and they use a keyword auto
Properties
- Default initial value of automatic variable is garbage
- visibility is the block were it is declared
- scope is within the block were variable is declared
In the above program the output will be 20garbage10, we know that visibility of a variable is only were it is declared so the variable a=20 is only seen within the inner block. Initialization of x is default so attempt to print x will be a garbage value. Outer block contain initialization of a as 10 so it is only visible outside block. Scope of each variable is within the block were it is declared.
static storage class
Static storage classes are allocate memory statically. The keyword static is used to declare static variable in c. Static is used with all datatypes such as int , float , char, double etc. Lifetime of static variable extends entire run of a program .
properties
- Default initial value of static variable is zero or null.
- Same static variable can be declared many times but initialized only once.
- If we declared static variable locally then its visibility will within a block where it has declared.
- If we declared static variable as globaly then visibility is only file in which it is declared.
- If static variable declared locally or globally scope is entire program.
The program below help you to use static variables
extern storage class
extern storage class let us to declare objects and functions that several source file can use. extern is a keyword used to declare external variables in c. Static is only know to the current file but extern means a global variable in one file also used for accessing other files.
properties
- It is default storage class of all global variables as well as functions in c.
- When we use extern keyword it is only declaration no memory allowed .To allocating memory it must be initilized
- Default initial value of external ariable is zero or null
- We annot initilize external variables locally ,it will produce a compiler error.
- Extern variable declared many time but initilized only once
register storage class in c
A register variables are placed on machine registers ie they store in cpu not in memory. Declaration as follows ,
register int a;
Here we requesting to compiler to store variable a in cpu and then compiler will decide were to store it. Compilers are free ti ignore the request .
- Register variable execute faster than other variable because it is stored in cpu
- CPU has limited amount of register so decision to select which variable as register is importent. So variable using many times can ctore in register
- It is not possible to take address of register variable it has not memory address.
- Default value of register variable is garbage
- Scope and visibilty is within the block




