C Programming(Chapter - 3)

in #technology7 years ago

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:

  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. 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

image

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

image

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

image

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

image

@naymyoaung
follow and vote for more chapter