Storage Class in C

In this section of the tutorial, we will discuss the behaviour of variables used in writing a program in C programming language.

In C language, a variable is not only is classified on the basis of its data type, but also on the basis of its storage class.

Now, we will learn about the storage class in C with illustrated examples for better understanding in detail below.

What is a Storage Class in C?

The concept of storage class determines the scope, visibility, and longevity of a variable.
It instructs the compiler about the location to store the variable, accessibility of the variable in the program, and value it stores at initializing the variable.

The scope of the variable determines at what region of the program a variable is available for use.
The visibility of the variable determines the accessibility of a variable from the memory.
The longevity of the variable determines the period or lifetime of the variable during which it retains a given value during the execution of a program.

The storage classes of variables in C are classified into four basic types:

  • Automatic Variables
  • External Variables
  • Static Variables
  • Register Variables

The variables are also classified into two types on the basis of the place of their declaration:

  • Local or Internal Variables – These variables are declared within a particular function in a program.
  • Global or External Variables – These variables are declared outside of any function in a program.

The concept of storage class is very crucial to understand the utility of a variable in order to develop multifunction programs.

A detailed list of scope and lifetime of Storage Classes of variables is given in the table below.

Further, we will discuss each class of variables in detail with a supporting example for better understanding.

Automatic Variables

Automatic variables are declared inside the function in which they are to be utilized. They are created when the function is called and they get automatically destroyed as soon as the function is exited.

  • Automatic variables are also called private or local variables because of the fact that they are declared inside the function.
  • An automatic variable can be optionally declared using the keyword auto :

auto int number;

  • A variable is by default classed as an automatic variable if it is declared without storage class specification.
  • Automatic variables can be used multiple times in different functions in the same program because of their useful property of not accidentally changing the value inside the variable. This eliminates any confusion between variables for the compiler.

Below is an example to demonstrate how automatic variables work.

 

Example:
#include<stdio.h>
void Add();
int main()
{
    int a,b,Sum;
    printf("Enter two numbers:");
    scanf("%d%d",&a,&b);
    Sum=a+b;
    printf("Sum of %d and %d is %d.",a,b,Sum);
    return 0;
}
Output:
Enter two numbers:5
2
Sum of 5 and 2 is 7.
Explanation:

In line 4 of the above code, variables a, b, and Sum of type int are automatic variables. The keyword auto is optional to use.

External Variables

External variables are declared outside a function, which means they can be accessed by any function in the program.

  • External variables are both alive and active throughout the entire program. Its scope and lifetime are valid for the entire duration of the execution of the program.
  • An external variable can be declared using the keyword extern :

extern int y;

  • External variables are also called global variables because of the fact that they can be used by every function present in the program.

Below is an example to demonstrate how external variables work.

Example:
#include<stdio.h>
void Add();
void Mul();

int x,y,n;

int main()
{
    Add();
    Mul();
    return 0;
}
void Add()
{
  
  extern int x,y,n;
  printf("Enter two mumbers:");
  scanf("%d%d",&x,&y);
  n=x+y;
  printf("Sum of %d and %d is %d.",x,y,n);
}
void Mul()
{
   
   extern int x,y,n;
   n=x*y;
   printf("\nMultiplication of %d and %d is %d.",x,y,n);
}
Output:
Enter two mumbers:5
2
Sum of 5 and 2 is 7.
Multiplication of 5 and 2 is 10.
Explanation:

In line 5 of the above code, variables x, y, and n of type int are external or global variables declared outside any function. It can be used in any function of the program.

In line 16, keyword extern is used to access the external variable x, y, and n of type int inside the function Add().

In line 26, keyword extern is used to access the external variable x, y, and n of type int inside the function Mul().

Static Variables

Static variables may be either an internal type or external type depending on the place of declaration, which means that they can be declared inside a function(internal type) or outside a function(external type).

  • The value of static variables is valid until the end of the program.
  • A static variable can be declared using the keyword static :

static int Age;
       static float Marks;

  • The internal static variables declared inside a function are similar to automatic variables, except that their longevity or lifetime is valid throughout the program. So, they can be used to retain values between function calls.

Below is an example to demonstrate how static variables work.

Example:
#include<stdio.h>
void Ascend();
int main()
{
    Ascend();
    return 0;
}
void Ascend()
{
  
  static int a;
  int Num;
  printf("Enter a number between 2 to 20: ");
  scanf("%d",&Num);
  for (a=1;a<=Num;a++)
  {
    printf("%d ",a);
  }
}
Output:
Enter a number between 2 to 20: 10
1 2 3 4 5 6 7 8 9 10
Explanation:

In line 11 of the above code, variable a of type int is a static variable accessed using the keyword static. It retains value between the function calls.

Register Variables

Register variables are frequently used variables that help in faster execution of the program. Hence, they are stored in the machine’s register.

  • A register variable can be declared using the keyword register :

register int count;

  • A register variable is stored in register whereas any other variable is stored in memory.
  • A register access is faster than a memory access, so storing frequently accessed variables in the machine’s register helps in faster execution of the program.
  • Most compilers only allow the storing of int and char type variables in the machine’s register.

Below is an example to demonstrate how register variables work.

Example:
#include <stdio.h>  
int main()  
{  
    
    register int Var;
    printf("The value of registered variable Var is %d.",Var);  
}
Output:
The value of registered variable Var is 0.
Explanation:

In line 5 of the above code, variable Var of type int is a register variable. It is stored in the machine’s register using keyword register and it has initial value equal to 0.

In the next section, we will practice a few examples of programs using functions.