storage classes/specifiers

To perform any task in the program, be it C++ or other programming language, we first declare the variable, which reserves the space in memory according to its data type.

The declaration of the variable specifies, what kind of variable it is and where it will take up space in the memory.

Using the storage class, we can determine where a variable will reserve space in memory and how long the program will be visible or active. In a way you can say that it determines how to specify storage to the variable.


Type of specifier

In C++ we can specify the variable 4 types of storage. Which are as follows-

 


auto storage specifiers

The variables that are declared within a function or block are called auto variables!

These are created in the variable program until the function remains in execution (the function in which it is declared). They are automatically destroyed as soon as the function definition terminates.

In this way you can say that all local variables are already auto variables. Therefore it is also called default storage class. If you declare a variable inside a function, it will be a variable of type by default auto storage (auto variable), that means the use of auto keyword here will be optional.

SYNTEX:

auto data-type variable-name;

Example:-

All the variables declared in the program below (i, f, s, name, lname) will be called auto variables.

 void main()
  {
    int i,f=0;
    auto int s=0;
    char name[]="Rahul";
     auto char lname[]="sherma";
    ............
    ...........
  }

Let’s try in an example below-

#include<iostream.h>
#include<conio.h>

void main()
 {
   clrscr();                 // all local variable are auto variable

   int i, f=3;
   auto int s=5;
   char name[]="Rahul";
   auto char lname[]="sherma";

   cout<<"\ninitial value: "<<i;           // Garbage value will be print
   cout<<"\nFirst value  : "<<f;
   cout<<"\nSecond value : "<<s;
   cout<<"\nComplete Name: "<<name<<" "<<lname;

   getch();
}
OUTPUT:-
initial value: -4314
First value  : 3
Second value : 5
Complete name: Rahul sherma


extern storage specifiers

Unlike the auto variable, those variables that are declared outside a function or block are called auto variables.

That is, all global variables are called extern variable. These can be accessed by any function or block of the program. Because they can be used according to their requirement in any function definition of the program. Which does not have to declare the extra variable inside the function. Which is a benefit to it.

Because all global variables are extern variable, so extern keyword (for global variable) is optional in it.

SYNTEX:

extern data-type variable-name;

All the variables declared in the program below (i,f,name,lname) will be called extern variables.

 extern int f=3;
 int i;
 char name[]="Rahul";
 extern char lname[]="sherma";

 void main()
  {
    ...........
    ...........
  }

Here is the program,

#include<iostream.h>
#include<conio.h>

  extern int f=3;
  int i;                  // automaticaly intialized to zero 
  char name[]="Rahul";
  extern char lname[]="sherma";
  
   void main()
    {
      clrscr();
      cout<<"intial value   : "<<i;             // print 0
      cout<<"\nFirst value  : "<<f;
      cout<<"\nComplete Name: "<<name<<" "<<lname;
      getch();
    }
OUTPUT:-
initial value: 0
First value : 3
Name : Rahul sherma

we can declare them as local variable only.


register storage specifiers

These are declared as locally in the auto variable but where the auto variable is stored in memory, the register variable is stored in the CPU register rather than saved in memory, so that they can be accessed sooner than other variables.

But if there is no space in the CPU register then at this stage, register variables will store in memory like auto variables.

register variable to declare the register keyword.

SYNTEX:

register data-type variable-name;

In below Program all of variables are called register variable

#include<iostream.h>
#include<conio.h>

void main()
 { 
  register int a;
  register int f=3;                // register variable declartion
  register char name[]="Rahul";
  register int i;                  //only declare not intialized

  clrscr();

  cout<<"\ninitial value: "<<i;
  cout<<"\nFirst value  : "<<f;    // Garbage value will be printed
  cout<<"\nName         : "<<name;

  getch();
}
OUTPUT:-
initial value: 97234
First value  : 3
Name         : Rahul

 

static storage specifiers

The static variable is declared both global and local.
When a static variable is declared locally, it can only be accessed from within the function (in which it is declared) but does not destroy even when the function is terminated and remains in the program.

The value of the global static variable is static for each function i.e. Their value does not change for execution of every function. The static keyword is used to create a variable static variable.

SYNTEX:

static data-type variable-name;

Let’s try to understand it with the help of a program.

#include<iostream.h>
#include<conio.h>

//global declration of static variable
static s=3;                   // static variable initialzation
static i;                  // automatic initialized to zero

void main()
 {
   clrscr();
   void first();
   void second();
   static int f=4;                   // local static variable

   clrscr();
   cout<<"\nFirst value : "<<i;
   cout<<"\nSecond value : "<<s;
   cout<<"\nlocal static variable: "<<f;

    first();
    second();

   getch();
 }

 void first()               // first function defination
  {
     cout<<"\nFirst function: "<<s;
    /* cout<<"\nlocal static variable: "<<f; */         //can't execute
  }
 
  void second()                   // second function defination
  {
    cout<<"\nsecond function: "<<s;
  }

OUTPUT:
First value : 0
Second value : 3
local static variable: 4
First function: 3
second function:3

Note: – Local variable is used by a singe function while global variable can be accessed by all functions.


previous-String library function and theirs examples

next-Object Oriented Programming in C++