Storage Classes in C

Storage Classes in C Language with Examples

In this article, I will discuss the Storage Classes in C Language with Examples. Please read our previous articles discussing Recursion in C Language with Examples. At the end of this article, you will understand the following pointer.

  1. Storage Classes in C
  2. How many types of storage classes are there in the c programming language?
  3. What is the automatic storage class?
  4. What is the static storage class?
  5. The auto Storage Class in C
  6. The register Storage Class in C
  7. The static Storage Class in C
  8. The extern Storage Class in C
Storage Classes in C Language:

In the C programming language, storage classes define the scope (visibility) and lifetime of variables and/or functions within a C program. There are four types of storage classes: C Storage classes provide the following information to the compiler:

  1. Storage area of a variable.
  2. Scope of variable. i.e., in which block is the variable visible?
  3. The lifetime of a variable, i.e., how long the variable will be there in active mode.
  4. The default value of a variable if it is not initialized.
How Many Types of Storage Classes Are There in the C Programming Language?

Depending on the behavior and storage area, storage classes are classified into two types, such as

  1. Automatic storage class
  2. Static storage class
What is the automatic storage class?

This storage class variable will be created automatically and destroyed automatically. Automatic storage class variables will be stored in the stack area of the data segment or the CPU register. Under the automatic storage class, we have two types of storage class specifiers, such as

  1. Auto
  2. register
What is the static storage class?

Static storage class variables will be created only once and throughout the program and will be in active mode. Static storage class variables will be there in the static area of the data segment. Under static storage class, we have two types of storage class specifiers

  1. Static
  2. extern

Please look at the following diagram for the classification of C storage classes.

Storage Classes in C with Examples

Auto Storage Class in C Language:

This is the default storage class for local variables. The keyword auto is rarely used as it’s the default behavior for local variables. Variables declared with auto are automatically allocated and deallocated when the function block in which they are defined is entered and exited, respectively.

The variable created using the specifier auto within a block is called an auto variable. The auto-storage class is the default storage class for all local variables. Let us see a program that will help us better understand the Auto Storage Class in C Language.

#include <stdio.h>
void abc();
int main()
{
   abc();
   abc();
   abc();
   return 0;
}
void abc()
{
    auto int a = 5;
    ++a;
    printf("\n a = %d ", a);
}
Output:

Auto Storage Class in C Language

The lifetime of the auto variable is restricted within the body; that’s why the number of times we call the function is that many times it will be created again and again. Let us see another program for more understanding.

#include <stdio.h>
void abc();
int main()
{
    auto int a = 5;
    ++a;
    abc();
    abc();
    ++a;
    printf("\n a = %d", a);
    return 0;
}
void abc()
{
    int a = 10;
    ++a;
    printf("\n a = %d ", a);
}
Output:

Storage Classes in C

According to the storage class of c, the scope of the auto variable is restricted within the body only. That’s why abc() function-related data can’t be accessed in the main() function. Let us see a program to understand this concept.

#include <stdio.h>
void abc();
int main()
{
    abc();
    abc();
    printf("\n a = %d", a);
    return 0;
}
void abc()
{
    int a = 10;
    ++a;
    printf("\n a = %d ", a);
}
Output:

How many types of storage classes are there in the c programming language

Register Storage Class in C Language:

The register storage class defines local variables that should be stored in a register instead of RAM. This means the variable has a maximum size equal to the register size and can’t have the unary ‘&’ operator applied to it (as it does not have a memory location). Modern compilers typically optimize the use of registers automatically, so the use of a register is often more of a hint than a directive.

It is a special kind of variable that is stored in the CPU register. The advantages of register variables are faster than the remaining variables.

Example to Understand Register Storage Class in C Language
#include <stdio.h>
int main()
{
   register int a = 10;
   ++a;
   printf("Value of a : %d", a);
   printf("\nEnter a value");
   scanf("%d" , a);
   --a;
   printf("\n Value of a : %d", a);
   return 0;
}

Output:
Value of a : 11
Enter a value 30
Value of a : 29

Note: The register storage class specifier first recommended to the compiler to hold the variable in the CPU register if the memory is available or stored in the data segment’s stack area.

In scanf() function, if the address is provided for the register variable, it will give an error. If the address is not provided, then it works normally.

Example to Understand Register Storage Class in C Language
#include <stdio.h>
int main()
{
   register int a = 10;
   ++a;
   printf("Value of a : %d", a);
   printf("\nEnter a value");
   scanf("%d" , &a);
   --a;
   printf("\n Value of a : %d", a);
   return 0;
}
Output:

Example to Understand Register Storage Class in C Language

The register should only be used for variables that require quick access, such as counters. It should also be noted that defining ‘register’ does not mean the variable will be stored in a register. It means that it might be stored in a register depending on hardware and implementation restrictions.

Limitations of Register Variables in C Language:

When working with register variables, we can’t create an “n” number of register variables, i.e., depending upon CPU capacity, 4-6 are the maximum variables we can create. In TC 3.0, we cannot access the address of register variables (but we can access them in GCC and dev c). Pointer or pointer-related concepts cannot be applied to register variables (in TC 3.0).

Static Storage Class in C Language:

The variable created using the specifier static is called a static variable. When working with the auto variables, the body restricts scope and lifetime. However, when we are working with static variables, the scope is restricted within the function, but the lifetime is not restricted.

  • When applied to local variables: The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes in and goes out of scope. Therefore, maintaining the value between function calls.
  • When applied to global variables: It limits the variable’s scope to the file in which it is declared. As a result, the variable or function is not visible outside this file.

The static storage class instructs the compiler to keep a local variable in existence during the program’s lifetime instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared. In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

Program to Understand Static Storage Class in C Language
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
int main() 
{
      while(count--) 
      {
      func();
      }
      return 0;
}
/* function definition */
void func( void ) 
{
   static int i = 5; /* local static variable */
   i++;
   printf("i is %d and count is %d\n", i, count);
}
Output:

Program to Understand Static Storage Class in C Language

Extern Storage Class in C Language:

The variable created using the keyword extern is called the extern variable. When we need to access a variable in more than one function, then go for extern variables, i.e., global variables, which are required. When working with global variables, scope and lifetime are not restricted. Whenever we declare a variable outside the function, it is called a global variable.

  • The extern storage class is used to give a reference of a global variable that is visible to ALL program files.
  • When you use the extern, the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.
  • When you have multiple files and define a global variable or function, which will be used in other files, then an extern will be used in another file to provide the reference of the defined variable or function. Just declare them with the extern keyword in another file.

The extern modifier is most commonly used when two or more files share the same global variables or functions, as explained below.

First File: main.c
#include <stdio.h>
int count;
extern void write_extern();
main() 
{
   count = 5;
   write_extern();
}
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void) 
{
   printf("count is %d\n", count);
}

Here, extern is being used to declare count in the second file, whereas it has its definition in the first file, main.c. Now, compile these two files as follows –

$gcc main.c support.c

It will produce the executable program a.out. When this program is executed, it produces the following result − count is 5

Summary of Storage Classes in C Language:

Storage Classes in C Language with Examples

Differences Between Auto, Register, Static, and External Storage Classes in C:

In C programming, storage classes specify the visibility (scope) and lifetime (storage duration) of variables and/or functions within a C program.

Auto Storage Classes in C:
  • Scope: Local to the block in which the variable is defined.
  • Lifetime: Exists only within the block in which it is defined. Automatically created when the block is entered and destroyed upon exit.
  • Default: All local variables are auto by default if no other storage class is specified.
  • Example Usage: Typically used for function parameters and local variables.
Register Storage Classes in C:
  • Scope: Local to the block in which the variable is defined.
  • Lifetime: Exists only within the block in which it is defined.
  • Purpose: Suggests to the compiler that the variable should be stored in a register instead of RAM for faster access. However, the compiler can ignore this suggestion.
  • Example Usage: Used for heavily used variables, like counters in loops.
Static Storage Classes in C:
  • Scope: For local variables: Local to the block defined by the variable, but unlike auto, the value persists between function calls. For global variables are restricted to the file in which they are declared.
  • Lifetime: Exists for the lifetime of the program.
  • Example Usage: Used for variables whose values need to be preserved between function calls and for file-level variables that should not be accessible outside the file.
External Storage Classes in C:
  • Scope: Global, accessible anywhere in the program.
  • Lifetime: Exists for the lifetime of the program.
  • Purpose: Used to declare a global variable or function in another file.
  • Example Usage: Useful for sharing data between different files in large programs.

In the next article, I will discuss the Auto Storage Class in C Language with Examples. In this article, I try to explain Storage Classes in C Language with Examples. I hope you enjoy this article on Storage Classes in C Language with Examples. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

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