C Programming(Chapter - 3)
Storage Class
A storage class is an attribute that tells us where the variable would be stored,
what will be the initial value of the variable if no value is assigned to that variable,
life time of the variable and scope of the variable.
Scope:- Scope of variable tells compiler the visibility of variable in the block
There are four storage classes in C:
- Automatic storage class
- Register storage class
- Static storage class
- External storage class
Automatic storage class:
keyword - ‘auto’.
variable declared stored in the memory.
Default - garbage value.
Scope - local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Example
Register storage class:
keyword -‘register’.
Variable declared stored in the CPU register.
Default - garbage value.
Scope of variable - local to the block
- The variable is stored in CPU register, it takes very less time to access that variable. Hence it becomes very time efficient.
- It is not necessary that variable declared as register would be stored in CPU registers. The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as automatic variable
- Unary operator[&] is not associated with it because value is not stored in ram.
- If we store same variable in the register memory than we can access that memory location directly without using the Address operator
Example
Static storage class:
keyword - ‘static’.
variable declared stored in the memory.
Default - zero.
Scope- local to the block in which the variable is defined.
Life of variable persists between different function calls.
Example
External storage class:
keyword - ‘extern’.
Variable declared stored in the memory.
Default - zero.
Scope - global.
-Variable is alive as long as the program’s execution doesn’t come to an end.
-External variable can be declared outside all the functions or inside function using ‘extern’ keyword.
Example
@naymyoaung
follow and vote for more chapter
nice post
done