STORAGE CLASSES IN C

Anuj Raghuwanshi - Aug 29 - - Dev Community

Hello everyone, This is my first post to dev.to in which I wanna tell you about STORAGE CLASSES IN C..

Storage classes in C are a set of keywords that guide the compiler in storing and manipulating variables in memory.
A Storage Class defines following attributes about a variable in C.
1.Scope :- Where will this variable be available.
2.Default initial value :- what will be the initial value
3.LifeTime :- Life of that variable in a program

There are FOUR Storage Classes in C which are most often used:
1.Automatic
2.External
3.Static
4.Register

Automatic Storage Class

A variable defined without any Storage Class specification is by default an automatic variable.

int variable_name and auto int variable_name are same.

Scope :- Local to the function body they are defined in
Default Value :- Garbage value
LifeTime :- Till the end of the function block they are defined in.

External Storage Class

They are same as global variable. External storage class in C, represented by the keyword 'extern', is used to give a reference of a global variable that is visible to all program files. When a variable is declared with the 'extern' keyword, it indicates that the variable is defined in another file and the current file is using its reference.
Extern variables cannot be initialised. They only point to an existing variable.
extern int variable_name

Scope :- Global to the program they are defined in
Default Value :- zero
LifeTime :- They are available throughout the lifetime of the program

Static Storage Class

Static storage class in C, represented by the keyword 'static'.
static int variable_name
static variable retains its value between function calls.Static variables are initialised only once.

Scope :- Local to the block they are defined in
Default Value :- zero
LifeTime :- throughout the program

Register Storage Class

Register variable requests the compiler to store the variable in the CPU register instead of storing it in the memory to have faster access.
register int variable_name

Scope :- Local to the function they are defined in
Default Value :- Garbage Value
LifeTime :- Available till the end of the function block in which the variable is defined.

. .
Terabox Video Player