Storage Class In C Programming Language

Storage Class In C Programming Language

In this course, you will learn about Storage Class, the range, and the lifetime of local and global variables. Additionally, static and register variables will be covered.

Storage Classes are used to describe a variable’s or function’s characteristics.

These characteristics, which primarily include scope, visibility, and lifetime, allow us to track the existence of a certain variable throughout the course of a program.

In C programming, each variable has two characteristics: type and storage class.

A variable’s data type is referred to as its type. Additionally, a variable’s scope, visibility, and lifetime are determined by its storage class.

The following 4 storage class types exist:

  • automatic
  • external
  • static
  • register

Local Variable(Storage Class)

Automatic or local variables are those declared inside a block. Local variables are only present within the block in which they are stated.

Take this as a case study.

#include <stdio.h>

int main(void) {
  
  for (int i = 0; i < 5; ++i) {
     printf("C programming");
  }
  
 // Error: i is not declared at this point
  printf("%d", i);  
  return 0;
}

An undeclared identifier I error message will appear when you run the program mentioned above. This is so because the for loop block contains a declaration of i. It’s undeclared outside the block.

Take yet another illustration.

int main() {
    int n1; // n1 is a local variable to main()
}

void func() {
   int n2;  // n2 is a local variable to func()
}

N1 is local to main() in the example above, and N2 is local to func ().

As a result, you are unable to access the n1 variable inside of func() because it is only present in main (). Similarly, since the n2 variable only exists inside func, you cannot access it inside main() ().

Global Variable(Storage Class)

External or global variables are defined as variables that are declared outside of any and all functions. They can be accessed from any program function.

Example 1: Global Variable(Storage Class)

#include <stdio.h>
void display();

int n = 5;  // global variable

int main()
{
    ++n;     
    display();
    return 0;
}

void display()
{
    ++n;   
    printf("n = %d", n);
}

Output

n = 7

Suppose file1 declares a global variable. The compiler will complain if you attempt to use that variable in a different file, file2. The term extern is used in file2 to indicate that the external variable is declared in a different file, which resolves the issue.

Register Variable

Register variables are declared using the register keyword. Local variables should be faster than registered variables.

Register variables can provide a small probability of speeding up your program, but modern compilers are pretty adept at code optimization.

There is no use for register variables unless you are dealing with embedded devices and are skilled at optimizing code for the specific application.

Static Variable

The static keyword is used to declare a static variable. For instance;

static int i;

A static variable’s value lasts until the program is finished.

Example 2: Static Variable

#include <stdio.h>
void display();

int main()
{
    display();
    display();
}
void display()
{
    static int c = 1;
    c += 5;
    printf("%d  ",c);
}

Output

6 11

The value of c is initialized to 1 during the initial function call. Its worth rises by five. The screen now displays the value of c, which is 6 at this point.

The second function call doesn’t initialize c to 1 once more. The reason is that c is a static variable. Five is added to the value of c. Its value is now 11 and is displayed on the screen.

To Practice, You Should Check Out Our Online Compiler: Compiler

Leave a Reply