Storage classes in C

Storage class in C programming defines scope and lifetime of a variable and function. At the time of variable declaration we define both data type as well as storage class of a variable.

C supports four storage classes.

  1. auto
  2. register
  3. static
  4. extern

auto storage class

auto is default storage class for all local variables.

Properties of auto storage class

  • Storage – auto variables are stored in memory (RAM).
  • Scope – Accessible only within the declared block.
  • Lifetime – Until control remains within the declared block.
  • Default value – auto variables are not initialized to a value by default. They contain some garbage value.

Note: auto is a C keyword use to define storage class of local variables. You must not use auto with global variables or functions, otherwise will generate compilation errors.

By default every local variable is declared as auto, hence adding auto keyword is pointless.

Syntax to declare auto variable

auto data_type variable_name;

Example to declare auto variable

int main()
{
    auto int num;
    float amount; // By default every local variable is auto

    return 0;
}

register storage class

register storage class tells the C compiler to store a variable in register instead of memory.

Since register variable is stored in CPU (register). Hence they do not have a memory location and guarantees a fast performance than other storage classes. In addition, we cannot store all our variables in registers because registers size is comparably very small to that of RAM. Also it is not guaranteed that the C compiler will allocate variables in register. Storing variables in register is hardware and OS dependent.

Many texts advocates that register variable has a faster access than other variables. However, this is not the real scenario. Modern compilers are essentially smart at code optimization for best performance.

Properties of register storage class

  • Storage – register variables are supposed to be stored in registers (CPU). However, it is not guaranteed.
  • Scope – Accessible only within its declared block.
  • Lifetime – Until program control remains within its declared block.
  • Default value – register variables are not initialized to a value by default. They contain garbage value as like auto variables.

Syntax to declare register variable

register data_type variable_name;

Example to declare register variable

register int counter;
register int i;

Note: You must not declare all your variables as register variable. Declare frequently used variables as register variable, such as loop counters.

static storage class

We use static keyword to define static variables and functions. It instructs C compiler to store a static variable in memory till the end of program. Rather than creating and destroying a variable on each function call.

C allocates memory for static variable once throughout the program and destroys it when program terminates. In addition, value of a static variable is preserved even after end of the function.

The static storage class can also be used with global variables and functions. It limits access of a global variable or function to all functions of same program.

Learn more about static scoped global variables in C.

Properties of static storage class

  • Storage – Static variables are stored in memory (RAM). They are stored in data segment of program instead of C stack.
  • Scope – Accessible only within its declared block.
  • Lifetime – Till the end of program.
  • Default value – static variables are initialized with zero, if not initialized explicitly.

Syntax to declare static variable

static data_type variable_name;

Example to declare static variable

#include <stdio.h>

/* Function declaration */
void display();

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

/* Function definition */
void display()
{
    int n1 = 10;

    /* static variables are declared only once */
    static int n2 = 10;

    printf("Local n1 = %d, Static n2 = %d\n", n1, n2);

    n1++; // Increment local variable
    n2++; // Increment static variable
}

Output –

Local n1 = 10, Static n2 = 10
Local n1 = 10, Static n2 = 11

extern storage class

We use extern (external) storage class to define reference to a global variable or function defined in some other C program. The extern modifier declares a link to its original variable or function declaration.

It is common to divide a big C program among various C files for easy maintenance, modularity and debugging. To share variables and functions among all these files we declare variables and functions in one C file, and use them in other C files through extern modifier.

Note: We are not allowed to initialize a variable if declared with extern modifier.

Properties of extern storage class

  • Storage – extern variables are stored in memory (RAM).
  • Scope – Accessible within its declared block.
  • Lifetime – Till the end of program.
  • Default value – extern variables are initialized with 0, if not initialized explicitly.

Syntax to declare extern variable

extern data_type variable_name;

Example to declare extern variable

Let us write two programs to demonstrate how to share variables and functions among various C programs using extern keyword.

First write and save file1.c

#include <stdio.h>

// Global variable
int num;

void display()
{
    int i;
    for(i=1; i<=num; i++)
    {
        printf("num = %d\n", i);
    }
}

Now write our main program and save it as main.c

/* Declare link to variable defined in file1.c */
extern int num;

/* Declare link to function defined in file1.c */
extern void display();

int main()
{
    // Access external variable
    num = 5;

    // Access external function
    display();

    return 0;
}

Since both programs are interlinked hence, you must compile both programs together. Use below command to compile both program at once.

gcc file1.c main.c -o main

Output –

num = 1
num = 2
num = 3
num = 4
num = 5