- Storage class decides the scope, lifetime and memory allocation of the variable.
- Moreover, The scope of a variable is the boundary within which a variable can be used.
- Four storage classes are available in C,
- Automatic (auto)
- Register (register)
- External (extern)
- Static (static)
- Variables which declared in a function of automatic storage class.
- Moreover, Automatic variables allocated storage in the main memory of the computer.
- Memory allocated automatically upon entry to a function and freed automatically upon exit from the function.
- Also, The scope of an automatic variable is local to the function in which it declared.
- Similarly, It is not required to use the keyword auto because by default storage class within a block auto.
- Example: int a; auto int a;
- Automatic variables allocated storage in the main memory of the computer; However, for most computers, accessing data in memory considerably slower than processing directly in the CPU.
- Moreover, Register variables stored in registers within the CPU where data can store and accessed quickly.
- Variables which are used repeatedly or whose access time should be fast may declare to be of storage class register.
- Also, Variables can declare as a register: register int var;
- Automatic and register variables have limited scope and limited lifetimes in which they declared.
- Moreover, Sometimes we need global variables which accessible throughout the program.
- extern keyword defines a global variable that visible to ALL functions.
- Moreover, extern also used when our program stored in multiple files instead of single file.
- Memory for such variables allocated when the program begins execution and remains allocated until the program terminates. Moreover, Memory allocated for an external variable initialized to zero. Also, Declaration for external variable is as follows: extern int var;
- static keyword defines a global variable that is visible to ALL functions in the same file.
- Moreover, Memory allocated for the static variable initialized to zero.
- Static storage class can specified for automatic as well as external variables such as static extern int varx; //static external static int var; // static automatic
- Also, Static automatic variables continue to exist even after the function terminates.
- The scope of static automatic variables is identical to that of automatic variables.