Storage Classes in C with Advanced Examples

In this tutorial, we will discuss the C storage classes with advanced examples. The purpose of storage class in a C program is to determine the initial value, the visibility, the lifetime, and the memory location of a given variable.

However, storage classes specify the scope and lifetime of any variable within a C language. These are to anticipate the types that are going to change.

Also read: Learn C Constant with Code Example Explanation

Four Types of C Storage Classes

  • Register Storage Class in C
  • Static Storage Class in C
  • External Storage Class in C
  • Automatic Storage Class in C

What is the Used of Storage Class in C?

The use of storage class in C has two properties, which are the storage class and type. For the storage class, it specified a variable lifetime and its visibility while the type specified any given variable data type.

Summary of Storage Classes in C

Class NameStorage PlaceScopeDefault ValueLifetime
AutomaticRAMLocalGarbage ValueWithin a scope
ExternalRAMGlobalZeroUntil the main program will end and it can acknowledge it anywhere in a program.
StaticRAMLocalZeroUntil the main program will end and it will maintain the available value between various function calls.
RegisterRegisterLocalGarbage ValueWithin a scope

Static Storage Class in C

The static storage class in C will give the instruction to the compiler to retain a local variable in continuation during the lifetime of the program. Rather, for creating and destroying it each time it will come into and go out within visibility.

Furthermore, the local variables static grant them to retain their values between the function calls. A static modifier may also apply to global variables. If this is done, it will cause the variables scope to be restricted to a file in which it is declared.

In a C language, if the static class is being used in a global variable, it will cause only a single copy of a member to be shared by all of the objects in its class.

Read or Visit the other interesting tutorial in C: Learn Variables in C with Code Example Explanation

For example:

#include <stdio.h>
void function(void);
static int calculation = 8; 
int main() {

   while(calculation--) {
   return 0;

void function( void ) {

   static int x = 5; /* local static variable */

   printf("x is %d and calculation is %d\n", x, calculation);


x is 6 and calculation is 7
x is 7 and calculation is 6
x is 8 and calculation is 5
x is 9 and calculation is 4
x is 10 and calculation is 3
x is 11 and calculation is 2
x is 12 and calculation is 1
x is 13 and calculation is 0

Features of static variables:

  • The variables that we specified as static variables are qualified of holding their assigned value that exists between different function calls.
  • The static local variables are only visible to the function in which we have specified them.
  • We can declare multiple times for the static variable, yet we can only assign it at one time.
  • In default, the initial value of a static variable is 0. Else, it is null.
  • We can use the static keyword in a case of a static variable.
  • The scope of any static global variable will stay in limited to that file in which we have declared it.

Let’s have a look at another example.


static float x;

static int y;

static char z;

static char s[200];

void main ()


printf("%d %d %f %s",x,y,z); // the initial default value of c, i, and f will be printed.



0 0 0.000000 (null)

Auto Storage Class in C

In C language, the auto or automatic storage class is called a default storage class in all local variables.

Let’s have a look at the example below.


int ages;

auto int age;


In a code above it defines the two variables, which are the same storage class. First, it can use a variable “auto” only within the scope or in the local variables.

Here are the features of auto storage class in C:

  • The allocation of memory for these variables will appears automatically through the runtime.
  • The visibility of an automatic variable is limited to a block.
  • The initial value of these variables is through, by a default or by a garbage value.
  • In an automatic variable the memory will be assigned free when it exits in a block.
  • We can use the auto keyword for defining the automatic variables.

Let’s have a look at the other example below.

#include <stdio.h>

int main()


int x; //auto

char y;

float z;

printf("%d %c %f",x,y,z); // to print the initial default value of the automatic variables p, q, and r.

return 0;



0 0.000000

Let’s have a look at another example below.

#include <stdio.h>

int main()


int x = 100,i;

printf("%d ",++x);


int x = 100;

for (i=0;i<3;i++)


printf("%d ",x); 




101 100 100 100 101

Register Storage Class in C

In C program, a storage variable can be used to define local variables that need to be stored in a register rather in a RAM(Random Access Memory). This means that the variable has a maximum size equal to the register variable size (its consists only one word). However, we can’t have the unary ‘&’ operator applied because it doesn’t have a memory location.

Also read: Learn C Data Types with Best Examples

Here is the example:


register int kilometers;


A register class must be used for variables that may have quick access like counters. We will also note that the defining register doesn’t mean a variable will be stored in a register. This means that it will be registered, it depends upon the hardware and implementing the restrictions.

In addition, here are the features of register storage class in C.

  • The variables we defined as the register it has a memory allocation in a CPU register. Actually, it depends upon the size of the memory that remains in a CPU.
  • The access time is faster than the automatic variables.
  • It means it cannot make use of the ‘&’ operator in a register variable.
  • By default, the value of given register will always be 0.
  • We can use the register keyword for a variable that should be stored in a CPU register. Although, the variable must be stored in a register or not is always a choice of a compiler.
  • We can quickly store the pointers in a register, which means that any register is qualified for storing the given variables.
  • It cannot store a variable into a register since we cannot apply more than one storage for the very same variable.

External Storage Class in C

In a C program, the external or extern storage class is used for giving a reference into global variables. Which is visible to all program files. When using “extern“, a variable cannot be initialized. Nevertheless, it points it to a variable name in a storage location that has been defined previously.

Furthermore, if we have multiple files and we defined a global variable or a function. The extern can also be used in another files to provide the reference of a defined variable. Take note that an extern is used for declaration of a global variable in other file.

It’s commonly used in the extern modifiers if there are two or more files sharing with the same global variables or functions.

Read also: Learn C Basic Syntax Rules with Examples

Let’s have a look at the example given below:

The first file we will name Main.c

#include <stdio.h>
int ascending;
extern void create_extern();
main() {
   ascending = 58;

The second file we will name the Support.c

#include <stdio.h>
extern int ascending ;
void create_extern(void) {
   printf("The ascending variable is %d\n", ascending );

In the example above, we have used the extern keyword for a declaration of the variable available in the second file. Unlike, its definition actually consists of the main.c (which is in the first file).

The compilations of these two files will be like this below:

$gcc main.c support.c


The ascending variable is 58


In conclusion, we learn the four types of storage classes, which are automatic or auto, external, static and the register storage class, the usage and we give the explanation for every example.

Leave a Comment