storage specifiers in C++

kind of variable it is and where it will take up space in the memory. By using storage classes in C++ we specify a variable’s memory location, their scope and visibility etc.

Let’s explain here,

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.

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 storage classes in c++

C++ Provided us 4 type storage class or specifier which are as follows-

  • auto storage specifiers
  • extern storage specifiers
  • register storage specifiers
  • static storage specifiers

auto in C++

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.

syntax

auto data-type variable-name;

Example of auto-variable

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

int main() //inside function
  {
    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>
using namespace std;
int main()
 {
                 // 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;
return 0;
}

OUTPUT

initial value: -4314
First value  : 3
Second value : 5
Complete name: Rahul sherma

Note: auto-variable stored in the stack memory location

extern in C++

unlike the auto variable, those variables that are declared outside a function or block are called extern 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.

syntax

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";

 int main()
  {
    ...........
    ...........
  }

Example of extern-variable

Here is the Program,

#include<iostream>
using namespace std;

//global variable
  extern int f=3;
  int i;                  // automaticaly intialized to zero 
  char name[]="Rahul";
  extern char lname[]="sherma";
  
   int main()
    {
      cout<<"intial value   : "<<i;             // print 0
      cout<<"\nFirst value  : "<<f;
      cout<<"\nComplete Name: "<<name<<" "<<lname;
   return 0;
    }

OUTPUT

initial value: 0
First value : 3
Name : Rahul sherma

we can declare them as local variable only.

Note: global variable stored in the data section or data segment memory.

register in C++

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.

declare a register variable register keyword will be used,

syntax

register data-type variable-name;

In below Program all of variables are called register variable

#include<iostream>
using namespace std;
int main()
 { 
  register int a;
  register int f=3;                // register variable declartion
  register char name[]="Rahul";
  register int i;                  //only declare not intialized

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

return 0;
}

OUTPUT

initial value: 97234
First value  : 3
Name         : Rahul

static in C++

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.

syntax

static data-type variable-name;

Example of static-variable

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

#include<iostream.h>
using namespace std;

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

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

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

    first();
    second();
return 0;
 }

 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:– static variable storage location depends on their declaration(locally and globally).


Previous – string library functions in C++ with examples
Next- what OOPs in C++


Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version