TeachingBee

Different Types of C Storage Classes And When To Use Them.

c storage classes

What Are C Storage Classes

C storage classes are representation of the visibility and location of the variable. It provides information from what part of the code can the variables be accessed. A storage class provides following information about variables:

  1. Scope of the Variable
  2. The location in which variables will be stored
  3. The value to which variable will be initialised
  4. Lifespan of variable
  5. Accessibility of the variable

There are majorly four kinds of C storage classes:

C Storage Classes
Automatic
Static
External
Register
Four Types Of C Storage Classes

Auto Storage Class

The variables that are defined by the auto storage class are referred to as local variables. Auto is a reference to an automatic storage class. Variables are in auto storage class by default even if it is not explicitly declared.

The scope for an auto variable’s scope is restricted to the block only. When the control is out of the block, access is removed. This means that only the block where it is stated, the variable auto is accessed by that block only.

The keyword “auto” is utilised to describe the class of auto-storage. By default an auto variable has the garbage value.

Uses

When developer wants to limit the access of variable to particular block. Auto storage class can be used.

// C Storage Classes : Auto

#include <stdio.h>

int main() {

  auto int j = 2;

  {
    auto int j = 4; 

    {
      auto int j = 5;
      printf("%d ", j);
    }

    printf("%d ", j);
  }

  printf("%d\n", j);
}

Output: 5 4 2

In the above example we can see j variable goes out of scope as soon as block ends.

External Storage class

Extern refers to the class of external storage. The term “extern storage class” is employed for variable or global function that are shared among several files.

Keyword extern can be used for declare a global variable or function in a different file in order to give the name to a variable or function that were previously defined in the file that was originally created.

The variables created using the extern keyword are referred to global variables. They are available throughout the program

If extern keyword is not used with global variables then compiler will automatically initialise to default value for that extern variable.

Note: When we use extern modifier with any variables it is only declaration i.e. memory is not allocated for these variable. To define a variable i.e. allocate the memory for extern variables it is necessary to initialise the variables.

Example 1.

// C Storage Classes : External

#include <stdio.h>
int count;    //By default it is extern variable and initialised to Zero
int main()
{
    printf("Value of count is %d",count);
    return 0;
}

Output: Value of count is 0

Example 2.

// C Storage Classes : External

#include <stdio.h>
extern int count;  
  //extern variable
int main(){
   printf("Value of count is %d",count);
    return 0;
}

Output: Compilation error, undefined symbol i.

Example 3.

// C Storage Classes : External

// First.c

#include<stdio.h>

int i=25; //By default extern variable
int j=5;  //By default extern variable
extern void sum();

void main()
{
    sum();
}


//Second.c

#include<stdio.h>
extern int i; //Declaration of variable i.
extern int j; //Declaration of variable j.
void sum(){
    int s;
    s=i+j;
    printf("%d",s);
}

Output: 30


The extern keyword in Second.c will search the initialisation statement of variable i and j either in Second.c (if initialised variable is static or extern) or First.c (if initialised variable is extern).

Static Storage Class

Static variables can be utilized in the function or file to create local variables. They may also be employed as a global variable.

  • A Static Local Variable is a local variable that is retained its value and is stored between blocks or function calls and is only visible to the block or function within which it is defined.
  • Global static variables refer to global variables that are visible only to the specific file that it is declared in.

Remember that the static variables have an default initial value of zero and can be initially created only once during its life.

The lifetime of static variables is within the entirety of program code. The variable declared or created using the static keyword will always have zero as the default value.

// C Storage Classes : Static

#include <stdio.h>

void next(void);

static int counter = 2; /* global variable */

int main() {
  while (counter < 5) {
    next();
    counter++;
  }
  return 0;

}

void next(void) {

  static int local_counter = 7; /* local static variable */
  local_counter++;
  printf("local_counter= %d and counter= %d\n", local_counter, counter);
}

Output: local_counter= 8 and counter= 2
local_counter= 9 and counter= 3
local_counter= 10 and counter= 4

Register Storage Class

It is possible to use the register storage class to save local variables inside functions or in blocks within registers on CPUs instead of RAM for rapid accessibility to the variables. 

The term register can be used in order to denote the class of register storage. The variables declared by register storage class will last through the duration of the program.

It’s similar to auto-storage class. The variable is restricted to the specific block. It is the only distinction is variables declared using register class are saved in CPU registers, not an actual memory. Register is faster to access than the main memory.

The variables declared using register storage class has no default value. They are typically declared at the start of the program. Also note that variable is not guaranteed to get register memory location, during which it behaves exactly like auto variable

// C Storage Classes : Register

void main(){

  register int register_var;
  printf("Before initialising is %d ", register_var); // Compilation error that register_var is not initialised
  register_var = 7;
  printf("After initialising is %d ", register_var);
}

Summary

C Storage ClassesKeywordStorageDefault ValueScopeLifeCycle
AutomaticautoStackGarbageWithin BlockEnd of Block
RegisterregisterCPU RegisterGarbageWithin BlockEnd of Block
ExternalexternData SegmentZeroGlobal Multiple FilesEnd of Program
Static(local)staticData SegmentZeroWithin BlockEnd of Program
Static(global)staticData SegmentZeroGlobal Within FileEnd of Program

In this article we covered various types of c storage classes. Hope you all got idea about difference and implementation of these c storage classes

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In