Storage class in C

Storage class :

Storage class in c defined the scope, lifetime, default value and the location of the variables where they will be declared.

There are four storage classes in c:

    • Automatic
    • Static
    • External
    • Register

Automatic storage class :

auto keyword is used for this. A variable is automatic by default if not specified else auto keyword is default.

Storage: Memory

Default value: garbage value.

Scope: They are local to the block in which they are defined.

Life: Till the control remains within the block in which it is defined.

Static storage class :

static keyword is used to make any variable or function static. It is used when we want value of variable to persist when the same function is called again.

Storage : Memory

Default value : 0.

Scope : Local to the block in which variable is defined.

Life : Value of variable persists between different function calls.

External storage class :

extern keyword is used. It is used when a variable is used in most of the functions or blocks

Storage : Memory

Default value : 0

Scope : Global

Life : Till program execution ends.

Register storage class:

register keyword is used.It provides faster access to the variables as they are stored in CPU registers. When CPU registers aren’t available or memory can’t be allocated to the variable then the compiler, itself, treats them as auto class variables. Thus, they are required when there is a frequent need of variable like in loops.

Storage : Registers

Default value : Garbage value.

Scope : Local to the block

Life : Till control remains within block.

Leave a Reply

Your email address will not be published. Required fields are marked *