Storage Class Specifiers in C and C++

Storage class specifiers are a way to tell compiler how to store the corresponding variable. There are following types of Storage class specifiers available in C/C++.

  • auto
  • extern
  • register
  • static
  • mutable — Only available in C++

“extern” Storage Classifier

The major use of “extern” is to tell compiler than an object/variable is declared somewhere else in the program and hence an external linkage is needed. Generally, a compiler looks for a variable definition in current block and then in global variables block. If it finds a match, then compiler attaches the reference to variable. “extern” keyword needed in case when variable is defined after it being used.

---- File one ----
int x,y;
int main ()
{
    /* … */
}

void fun1 ()
{
    x = 100;
}

---- File Two ----
extern int x, y;
void fun2 ()
{
    x = y / 10;
}

void fun3 ()
{
    y = 50;
}
int main ()
{
    extern int x, y; /* Here extern is needed as variable defined later */
    x = 10;
    y = 20;
}

int x, y;

“static” Storage Class Specifiers

Static variables are permanent variables within their own function or file. Unlike global variable these are unknown outside their function/files. However, they maintain their values between calls.
Static variables can be of following types:

“static” Local Variable

When Static specifier is being applied on local variable, storage is being created like a global variable. However, it remains known only to the block in which it is declared.

int fun ()
{
    static int demo;
    static int num = 100;
    num = num + 11;
    return num;
}

“static” Global Variable

When static specified is applied in global variable then compiler creates a global variable which is known only to the file in which it is declared.

static int global; 
int main ()
{
    int demo;
    int num = 100;
    num = num + 11;
    return num;
}

“register” Storage Class Specifiers

When register specifier is applied to a variable then compiler is being requested to keep the variable value in a register of the CPU. This is needed in cases where access of the object should be as fast as possible.

int main ()
{
    register int counter = 0;
    return 0;
}

Important Points

  • It is very much possible that compiler ignores the register specifier altogether.
  • register specifier can only be accessed to local or formal parameters in a function.
  • register specifier can’t be used with global variable.

“auto” Storage Class Specifiers

This is the default storage class for all the variables which can be used to declare local variables. Since all nonglobal variables are assumed to be auto, this keyword is virtually never used.

“mutable” Storage Class Specifiers

mutable storage class specifiers are only available in C++. This specifier is mainly used for class data member to make it modifiable even though the object is defined as const.

class X {
    public:
        mutable int x;
        int y;

        X (int a, int b): x(a), y(b)
        {}

        int f1 () const
        {
            x = x * x; // This works as x is mutable
        }  
};
int main ()
{
    const X obj(10,200);
    obj.x = 400; // works as x is mutable
    obj.y = 1000; // Error as obj is const object.
}

Leave a Reply

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