C Programming Tutorial
About Lesson

C Storage Class

In this tutorial, we will learn about Storage classes and its type in C programming with the help of some examples.


Storage Class

Each variable in C programming has two properties :

  1. Type
  2. Storage Class

Type is used to determine the type of data store in that variable (Datatype) .

Storage Class are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which helps us to trace the existence of a particular variable during the runtime of a program.

Every variable in C++ has two features: type and storage class. Type specifies the type of data that can be stored in a variable. For example: intfloatchar etc.


Types of storage classes :

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

auto Storage Class

The variable defined using auto storage class is called Automatic variable.
They are also known as local variable.

An auto storage class is default class. if we have declared a variable within a function without defining any storage class then variable automatically promoted to auto storage class.

Some properties related to an auto variable

Scope: Within the block or function in which it declares.
Keyword : Keyword used to define automatic variable is auto.
life:  Live till the control remains in function.
storage: auto variables are stored in RAM (Random Access Memory).
Default value:  Garbage.

Syntax :

auto int varible_name;

Example :

#include <stdio.h>
int main() {
  auto int a = 10;
 {
    auto int a = 11;
    {
      auto int a = 12;
      printf("%d ", a);
    }
    printf("%d ", a);
 }
  printf("%d ", a);
}

Output :

12 11 10 

In the above example, you must have observe that the value of the auto int a; is only limited to only the end of the function/block in which it is declared .


extern Storage Class

The variable defined using extern storage class is called Global variable.
They are global variables or functions which are shared between two or more files.

Some properties related to the global variable

Scope: It accesses by all function.
Keyword : Keyword used to define Global variable is extern.
life: life of the global variable is the throughout of the execution of the program.
storage: If the global variable is initialized then stored in .ds otherwise stored in .bss.
Default value: The defaults value of the global variable is Zero.

Syntax :

extern dataType varible_name;

Example :

First File : main.c

#include <stdio.h>
extern int num;
void main() {
  printf("The value of the external integer num is %d", num);
}

Second File : second.c

#include <stdio.h>
num = 40;

Output :

The value of the external integer num is 40

In the above example, you must have observe that the extern int num is decared in main.c and can also be accessed or initialized in second.c .


static Storage Class

The variable defined using static storage class is called Static variable.
They can be assigned only once and exist till the termination of the program.

Some properties related to the global variable

Scope: the scope within the function in which declare. But if the global variable is static then the scope of that variable limited to the file in which it declares.
Keyword : Keyword used to define Static variable is static.
life: life of static variable whole execution of the program..
storage: if the static variable is initialized then stored in .ds otherwise stored in .bss.
Default value: the default value of the static variable is Zero.

Syntax :

static int varible_name;

Example :

#include <stdio.h>
static int num1 = 10;
void main() {
while(num1 < 15) {
other();
num1 ++;
}
}
void other() {
  static int num2 = 20;
  num2++ ;
  printf("The value of num1 is %d, num2 is %d \n", num1, num2);
}

Output :

The value of num1 is 10, num2 is 21
The value of num1 is 11, num2 is 22
The value of num1 is 12, num2 is 23
The value of num1 is 13, num2 is 24
The value of num1 is 14, num2 is 25

In the above example, you must have observe that the static int num1 is decared global variable and can be accessed from anywhere in the program. Then static int num2 is declared in other() function can only be accessed in other() function.


register Storage Class

The variable defined using static storage class is called Static variable.
The register storage class is used to define local variables and the size of register storage class has a maximum size equal to the register size .

Some properties related to the global variable

Scope: The scope and visibility of the register storage class is limited to function/block in which it is defined.
Keyword : Keyword used to define Register variable is register.
life: The lifetime of Register variable is till the end of function/block in which it is defined.
storage: Register variables are stored in Register.
Default value: The register storage class is initialized to garbage value by default.

We cannot get the address of the Register variable.

Syntax :

register int varible_name;

Example :

#include <stdio.h>
void main() {
  register int num = 20;
  printf("The value of register variable num is %d \n", num);
}

Output :

The value of register variable num is 20

In the above example, register variable num is declared.


Storage class table

Storage Class Declaration Storage Default Initial Value Scope Lifetime
auto Inside a function/block RAM Garbage Value Within the function/block Within the function/block
extern Outside all functions RAM Zero Everywhere in the program Till the end of the program
Static (local) Inside a function/block RAM Zero Within the function/block Till the end of the program
Static (global) Outside all function/block RAM Zero Global Till the end of the program
register Inside a function/block CPU register Garbage value Within the function/block Within the function/block

 

error: Content is protected !!