Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » C Programming » Tutorial » Storage Classes

Storage classes in C Programming

Storage class specifiers in C tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable. By default variables belongs to "auto" storage class in C programming.

Syntax of declaring storage class

storage_class data_type variable_name;

Type of Storage Classes C Programming

  • auto
  • static
  • register
  • extern

auto- The scope of this auto storage class is within the function only. Keyword "auto" is used to declare variables as auto variables. All local variables are auto variables by default.

static- This storage class is used to declare static variables. Static variable have the feature of preserving their values even after they are out of their scope.

register- Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU main memory. Register variables will be accessed very faster than the normal variables since they are stored in register memory rather than main memory. But, only limited variables can be used as register since register size is very low.

extern- The scope of this extern variable is throughout the main program. It is equivalent to global variable. Definition for extern variable might be anywhere in the C program or in another header file.

Example of auto storage class

#include<stdio.h>

void myfunction()
{
auto int a = 10;
printf("%d, ",a);
a++;
}

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

If you look at the above example then 'int a' is declared as auto; which means every time we call myfunction(), the variable get memory allocated in memory and then destroyed when controll goes out of function. The output of above program is 10,10,

Example of static storage class

#include<stdio.h>

void myfunction()
{
static int a = 10;
printf("%d, ",a);
a++;
}

void main()
{
myfunction();
myfunction();
}

If you run the above program then output would be 10,11, its like that because Static variables retain the value of the variable between different function calls.

Example of register storage class

#include<stdio.h>

void main()
{
register int i;

for (i = 0; i<7; i++)
{
    printf("\n CppBuzz.com");
}

}

If you look the above program we have declared variable "i" as register because it is being use frequently. Ideally we should use register storage class with loop variables. For faster access of a variable, it is better to go for register specifiers rather than auto specifiers. Reading and Writing to register variable is faster. Because, register variables are stored in register memory whereas auto variables are stored in main CPU memory. Only few variables can be stored in register memory because of memory size limitation. Ideally, we should use variables as register if they are used very often in a our C program like in loop.

Example of extern storage class

#include<stdio.h>
 
int main( )
{
extern int myy;
printf("The value of y is %d",myy);
return 0;
}
int myy=50;

Another Example on Extern Storage Class

//define some variable in another header file
//content of cppbuzz.h
int xyz = 10;

//Now use this header file on other program

#include<stdio.h
#include "cppbuzz.h"

int main( )
{
extern int xyz
printf("The value of y is %d",xyz);
return 0;
}