The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

C++ Storage Classes

We have seen that every variable has a data type. To fully define a variable, we need a storage class as well apart from data type. But you might be thinking that we haven't used any storage class so far. Actually, we were using a default storage class till now. When we do not define a storage class, compiler automatically assigns a default storage class to it. A variable's storage class gives information about the storage location of variable in memory, default initial value, scope of the variable and it's lifetime.
Storage class specifiers supported in C++ are auto, register, static, extern and mutable.

' auto ' storage class
This is the default storage class we have been using so far. It applies to local variables only and the variable is visible only inside the function in which it is declared and it dies as soon as the function execution is over. If not initialized, variables of class auto contains garbage value.
Following example shows how variables of auto storage class are declared :

int var; // by default, storage class is auto
auto int var;

' register ' storage class
Variables of class 'register' are stored in CPU registers instead of memory which allows faster access. The scope of the variable is local to the function in which it is defined and it dies as soon as the function execution is over. It contains some garbage value if not initialized. It is declared as :

register int var;

' static ' storage class
The scope of static variable is local to the function in which it is defined but it doesn't die when the function execution is over. The value of a static variable persists between function calls. The default initial value of static variable is 0. Following program illustrates the behaviour of static variables :

#include<iostream>
using namespace std;

void func() {
   static int count = 0;
   /* value of 'count' persist between function calls
    * i.e count doesn't become 0 every time func() is called;
    * previous value of count remains alive
    */
   count++;
   cout << "Function is called " << count << " times " << endl;
}

int main() {
   cout << "Calling Function :- " << endl;
   func();
   cout << "Calling Function :- " << endl;
   func();
   cout << "Calling Function :- " << endl;
   func();
   return 0;
}

' extern ' storage class
Variables of extern storage class have a global scope. We use extern variables when we want a variable to be visible outside the file in which it is declared. So, an extern variable can be shared across multiple files. An extern variable remains alive as long as program execution continues. A static global variable is visible only in the file it is declared but an extern global variable is visible across all the files of a program.
We have used declaring and defining a variable interchangeably but extern storage class adds a distinction.
In simple words :

extern int count; // declaration of variable 'count'
int count; // definition of variable 'count'

The declaration and definition of a variable can be in different files.
Following program shows the use of extern storage class :

/*
 * File : extern_var.cpp
 */

#include<iostream>
#include "extern_var_util.cpp" // including the contents of a user defined file
using namespace std;

extern int count; // declaration of variable 'count'

int main() {
   cout << "count : " << count << endl;
   count = 1;
   incrementCount();
   cout << "count : " << count << endl;
   return 0;
}
Run this program in your system  
/*
 * File : extern_var_util.cpp
 */

int count = 7; // defination of variable count

void incrementCount() {
   ++count;
}
Save this piece of code in the same directory as the above program  

We can observe that the variable count is declared in one file and it is defined in other file. It can be accessed and modified in any of the files and the updated value of count is reflected in both the files.

' mutable ' storage class
mutable storage class is applicable to only class data members. We will learn about classes and objects later in the tutorial but we cover mutable storage class for the sake of completeness. For the time being, just remember that class is a user defined data type and object is a variable of type class. We know that a constant variable cannot be modified in the program. This is applicable to objects also. If a data member of a class is declared as mutable, then it can be modified by an object which is declared as constant. See the program below to get more clarity :

#include<iostream>
using namespace std;

// defining a class 'X' (user-defined data type)
class X {
public :
   X (int a, int b) { // constructor
      // data member initialization
      m = a;
      n = b;
   }
   // data members
   int m;
   mutable int n;
};

int main() {
   // declare a const variable 'obj' of type 'X'
   const X obj(5, 2); // m = 5 and n = 2
   cout << "m : " << obj.m << "  n : " << obj.n << endl;
   // obj.m = 7; // illegal since 'obj' is constant
   obj.n = 8; // legal since 'n' is mutable
   cout << "m : " << obj.m << "  n : " << obj.n << endl;
   return 0;
}

If you haven't understood the use case of ' mutable ', you can come back here after getting hold of the concepts related to classes and objects discussed later in the tutorial.

Back | Next