Storage Classes

C Storage class

In C language, storage classes are categories on basis of  permanance of  variable and scope of the variable within the program. There are four types of  storage classes.

  1. Automatic variables
  2. External variables
  3. Static variables
  4. Register variables

1. Automatic variables

Automatic variables is always local to a function. Automatic variables always declared within a function. Scope of  of automatic variable is restricted to a function only where it is declared. Automatic variables does not retain its value.  All variable declared within a function is act as local variable of this function.

Advantages of  using auto variables

  • Automatic variable are always local to the function in which it is declared, so we can define variable with same name in other functions.
  • Their scope is confined to that function in which it is declared, so it can not be access from other functions.

Auto keyword is used to specify automatic variable but it is not necessary. See example below.

  1. #include<stdio.h>
  2. int gt_add(); /* Function prototype */
  3. int main()
  4. {
  5.      /* value is automatic variable to function main() */
  6.     int value = 0;
  7.     /* Add function call with no argument */
  8.     value = gt_add();
  9.     printf(“value = %d\n”,value);
  10.     return 0;
  11.  }
  12. int gt_add() /* Function definition */
  13. {
  14.     /* num1 , num2 and sum is automatic variable to function gt_add */
  15.     int num1 = 0;
  16.     int num2 = 0;
  17.     int sum = 0;
  18.     scanf(“%d”,&num1);
  19.     scanf(“%d”,&num2);
  20.     sum = num1 + num2;
  21.     printf(“num1 = %d\n”,num1);
  22.     printf(“num2 = %d\n”,num2);
  23.     printf(“sum = %d\n”,sum);
  24.     return sum;
  25. }

2. External variables

External variable is a global variable. It is a variable defined outside of any function block. External variable is not associated with a single function. It is possible to define variables that are external to all functions, that is, variables that can be accessed by same name by any function. If  we change value in one function will reflect in another function. External variable declaration must begin with ‘extern” keyword. Scope of external variable is through out the program.

Before using the external variable, i would like to explain difference between definition and declaration of external variables.

Definition of external varaibles

  • External varaibles definition is similar to ordinary variable declaration.
  • External varaibles defines before function definition that access it.
  • “extern” keyword not required for external variable definition.
  • Required memory automatically  allocated for an external variable definition.

Declaration of external varaibles

  • An external variable declaration must begin with keyword ” extern “

Advantages of  using external variables

  • Retain their value through out the program.
  • External variable can access from any function within their scope.

3. Static variables

Static variables is like automatic varisble but it must begin with “static” keyword and retain thgeir values throught out the life of program.

Advantages of using static variables

  • Retain their values through out the program.
  • Zero’s assigned by default, if static variables is not assigned to a value at the time of decalaration.
  • Static variables declare in one file can not be access from other files same if a static variabe declare inside a function can not access by other functions directly.

4. Register variables

Also register variables is like automatic variables but it must begin with “register” keyword. Values of register variables are stored in CPU registers. Scope of the register variables is within a function only.

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.