Storage class defines the scope (visibility) and lifetime of variables and/or function declared within a C program. In addition to this, the storage class gives the following information about the variable or the function.

  • The storage class of a function or a variable determines the part of memory where storage spaces will be allocated for the variable or the function (whether the variable/ function will be stored in a register or in the RAM).
  • It specifies how long the storage allocation will continue to exist for that function or variable.
  • It specifies the scope of the variable or the function, that is, the storage class indicates the part of C program in which it is accessible.
  • It specifies whether the variable or function has internal, external, or no linkage.
  • It specifies whether the variable will be automatically initialized to zero or any indeterminate value.

C supports four storage classes:

  1. Automatic
  2. Register
  3. External
  4. Static

The general syntax for specifying the storage class of a variable can be given as:

<storage_class_specifier> <data_type> <variable_name>

1- Automatic storage class:

The auto storage class specifier is used to explicitly declare a variable with automatic storage. It is the default storage class for variables declared inside a block.

Example: auto int x;

X is an integer that has automatic storage. It is deleted when block in which x is declared is exited.

The auto storage class can be used to declare variables in our block or the names of function parameters. However, since the variable names or names of the function parameters by default have automatic storage, the auto storage class specifier is therefore treated as redundant while declaring data.

 Important things to remember about variables declared with auto storage class are as follows:

1- All local variables declared within a function belong to automatic storage class by default.

2- They should be declared at the start of the program block, right after the opening curly bracket ({).

3- Memory for the variable is automatically allocated to upon entry to a block and freed automatically upon exit of the block.

4- The scope of the variable is local to the in which it is declared. These variables may be declared within a nested block.

5- Every time the block (in which the automatically variable is declared) is entered, the variable is initialized with the value declared.

6- The auto variables are stored in the primary memory of the computer.

7- If auto variable are not initialized at the time of declaration, then they contain some garbage value.

The following code uses an auto integer that is local to the function in which it is defined.

#include<stdio.h>

void func1()

{

    int a=10;

    printf(“\n a=%d”, a);

    //auto integer local to func1()

}

Void func2()

{

    int a=20;

    printf(“\n a=%d”, a);

    //auto integer local to func2()

}

Void main()

{

    int a=30;

    func1();

    func2();

    printf(“\n a=%d”,a);

}

Output:

a=10

a=20

a=30

2- Register Storage Class:

When a variable is declared using register as its storage class, it is stored in CPU in register instead of RAM. Since the variable is stored in a register, the maximum size of the variable is equal to the register size. One drawback of using a register variable is that they cannot be operated using the unary ‘&’ operator because it does not have a memory location associated with it. A register variable is declared in the following manner:

Register variables are used when quick access to the variable is needed. It is not always necessary that the register variable will be stored in the register. Rather, the register depending on the hardware and implementation restriction.

Hence, programmers can only suggest to the compiler to store those variables in the register which are used repeatedly or whose access times are critical. However, for the compiler, it is not an obligation to always accept such requests. In case the compiler rejects the request to store the variable in the register, the variable is treated as having the storage class specifier auto.

Like auto variable, register variable also have automatic storage duration. That is, each time a block is entered, the register variables defined in that block are accessible and the moment that block is exited, the variables become no longer accessible for use. Now let us have a look at the following code that uses a register variable.

#include<stdio.h>

int exp(int a, int b);

int main()

{

    int a=3,b=5,res;

    res=exp(a,b);

    printf(“\n %d to the power of %d=%d”,a,b,res);

    return 0;

}

int exp(int a, int b)

{

    register int res=1;

    int i;

    for(i=1;i<=b;i++)

              res=res*a;

    return res;

}

Output: 3 to the power of 5= 243.

3- Extern storage Class:

A large C program can be broken down into smaller programs. When these smaller programs are compiled, they are joined together to form a large program. However, these smaller programs may need to share certain variables for processing. For such situations C language provides an external storage class that is specified C language provides an external storage class that is specified using the keyword extern.

The extern storage class provides the reference to the global variable that would be visible throughout the program. Such global variables are declared like any other variable in one of the program files. When there are multiple files in a program and you need to use a particular function or variable in a file apart from which it is declared, then use the extern keyword. To declare a variable x as extern writes:

extern int x;

External variables have global scope. i.e., these variables are visible and accessible from all the functions in the program. However, if any function has a local variable with the same name and types as that of the global or extern variable, then references to the name will access the local variable rather than the extern variable. Hence extern variables are overwritten by local variables.

// FILE 1.C

#include<stdio.h>

#include<FILE.C>

//Programmer’s own header file

int x;

void print(void);

int main()

{

x=10;

printf(“\n xin a FILE1=%d”,x);

print();

return 0;

}

//END OF FILE1.C

//FILE2.C

#include<stdio.h>

extern int x;

4- Static Storage Class:

The features of a variable defined to have a static storage class are as under explained:

Storage: Memory

Default value: Zero

Scope: Local to the block in which the variable is defined.

Life: Value of the variable persists between different function calls.

Compare the two programs and their output in the figure:

Storage Classes in c programming Language for BeginnersUnderstand the difference between the automatic and static storage classes.

The program written in the figure consists of two functions that is main () and increment(). The function increment () gets called from main() three times on the one go. Each time it prints the value of I and then increments it. The only difference in both of the programs is that one of them uses auto storage classes for the variable i, whereas the other one uses static storage class.

Like auto variables, static variables are also local to the block in which they are declared. The difference between them is that static variables don’t appear when the function is no longer active.