Storage classes in C ++

Storage Classes in C++

Storage classes refer to the life time of the variables. More precisely it means that what is the scope of the variables. Storage classes restrict the visibility of variables so the variables must be used within the allowed scope.

Storage classes refer to the life time of the variables. More precisely it means that what is the scope of the variables. Storage classes restrict the visibility of variables so the variables must be used within the allowed scope.

The storage classes available in C++ are as below.

• Local
• Global
• Register
• Static
• Extern

Local Variables

Variables defined within a function are called local variables of that function. Those variables can only be used in the function where defined and are not visible in other functions. For example in the following code segments, variables a, b are declared in function1 so are local variables and must be used within function1.

These variables are not visible outside the function1 so cannot be used in other functions. If we want to use these variables in any other function then they must be re-declared there and there will be no relation among these variables. Similarly variables x, y are declared in function2 and thus are local for this function. The life time and scope of these variables are limited to function2 only.

void function1()
{
int a,b;
.
.
}
void function2()
{
int x,y;
.
.
}

Global Variables

Variables defined outside the functions are called global variables. As these variables are not property of a specific function so are visible to all the functions of the same program as shown in the following program segment.

int a;
void function1()
{
a=10;
.
.
}
void function2()
{
cout<<a;
.
.
}

We can see that variable a is declared before any function and thus can be used in all the following functions. In function1 a is assigned a value and in function2 the value of a is displayed.

Register Variables

Register variables are special kind of local variables. These variables are always defined within a function and are processed faster than other variables because they are stored in registers rather than RAM. Register variables are declared like normal variables preceded by register keyword as below.

register int a;

While declaring register variables we must keep in mind that we can not use pointers for these variables which are required in advanced C++ programming (pointers will be discussed in a later tutorial). So if the only concern is the speed then these variables should be used.

Static Variables

All the local variables of a function release their memories and are initialized again if the function where they are defined are called again. It means that once the function is finished, the variables declared by that function are removed from the memory but this is not the case with static variables. Static variables retain their values in memory and are not initialized every time the function is called. When we call a function having static variables, the last value of those variables are used as shown in the example below.

#include “conio.h”
#include “iostream”
using namespace std;

void myFunction()
{
static int counter=1;
cout<<“You called this function “<<counter<<” times”<<endl;
counter++;
}
void main()
{
myFunction();
myFunction();
myFunction();
getch();
}

The output of the above code is as below.

function 123

We can see from the above output that variable counter is not initialized every time the function myFunction is called. If we remove the static keyword from above code then the output becomes as below because the variable counter is initialized every time we call the function myFunction.

function1

Extern Variables

Extern (external) variables are kind of variable defined in one file and useable in another file. For instance, if we have a program saved in file1.cpp having extern variables can be used in another program as below.

file1.cpp
#includeconio.h”
#include iostream”
using namespace std;
extern double PI=22/7;



file2.cpp
#include “conio.h”
#include “iostream”
#include “file1.cpp”
using namespace std;
double PI;
void main()
{
PI=22/7;
Cout<<”PI= all();
cout<<“PI= “<<PI<<endl;
getch();
}

In the above code, we can see that variable PI is declared as an extern variable in file1.cpp and is used in file2.cpp. It is important to include file1.cpp in file2 otherwise the external variables will not work.

More Related Articles For You

    C++ Tutorial

    C++ Quizzes