Storage Classes in C

Storage class determines the amount of memory allocated for the variable and how long the storage allocation continues to exist. It also determines the part of program over which that variable is visible.
Four storage classes in C:
1. Auto
2. Register
3. External
4. Static

Auto Storage Class: It is default storage class in C. These variables are declared inside the function in which they are to be used. They are created at the time of function call and destroyed automatically when we exit from the function. If automatic variables are not initialized, they will contain garbage value.

Extern Storage Class: These variables are declared outside the function. Generally, known as Global Variables and default value is zero.

  • Extern declaration does not allocate storage space for variables.
  • If any variable is declared as extern, then compiler checks for that variable initialization in the program. If that variable is not initialized, then compiler will show an error.
  • Any extern variable can be declared as many times as you can but it can be initialized only one time.
Any assignment can't be written globally.
Example:    i=5;
Note: Assigning any value to the variable at the time of declaration is known as initialization while assigning any value to the variable not at the time of declaration is known as assignment.

Register Storage Class: Variables are stored on one of the machine's registers and are declared by using the keyword 'register'. They enables faster access of variables and faster execution of the program. C can automatically convert register variable to non-register variable once the upper limit is reached.

Static Storage Class: Value of static variables persists until the end of the program. It may be of external or internal type depending on the place of its declaration. Static variables are initialized only once when the program is compiled.

Comments

Popular posts from this blog

Three mislabeled Jar

Difference between Macro And function