Chapter 7: Storage Class

Storage class is a location, scope or longevity specifier of a variable.

  • There are four different storage-class specifications:
    Automatic
  • External
  • Static
  • Register

Automatic
Keyword: auto

Syntax: auto;

Example:
auto int a;
Initialization: Not done by the compiler automatically, to be done by the programmer.
Scope: Confined to a single function in which it is declared.
The variable automatically created and destroyed within a function is called Automatic variable. That means an automatic variable does not retain its value once control is transferred out of its defining function. The default storage class of a variable declared inside a function is automatic. That is why the keyword auto is not required at the beginning of each variable declaration. The automatic variable is also referred to as local or internal variable.

 

External (Global)
Keyword: extern
Syntax: extern;

Example:
extern int a;
Initialization: Done by the compiler automatically to 0, if not done by the programmer.
Scope: Extends from the point of declaration through the remaining part of the program.

The variable that is recognized globally and can be accessed from any function that falls within its scope is called External variable or Global variable. This implies that an external variable declared somewhere…

I would like to learn more!