C++ Storage Classes

C++ Storage Classes


C++ Storage Classes
Storage class specifiers tell compiler the duration and visibility of the variables or objects declared, as well as, where the variables or objects should be stored.
In C++ program we have multiple files. In these files we may have normal variables, array, functions, structures, unions, classes etc. So, variables and objects declared must have the visibility, the lifetime and the storage, when value assigned.
Storage Classes
In C\C++ there are four different storage classes available:
automatic, external, static, register.
It is similar in C++
Storage Classes                     keyword
Automatic                             auto
External                                extern
Static                                    static
Register                                register
 
Automatic Variable - auto
Local variables are variables declared within a function or blocks (after the opening brace, { of the block } ). Local variables are automatic by default. This means that they come to existence when the function in which it is declared is invoked and disappears when the function ends.
The Syntax of Static Variable is as follows:
            auto int a, b, c = 30 auto char firstname;
-same as
            int a, b, c = 30 auto char firstname;
External Variable - extern
Local variables are variables that are recognized globally, rather than locally. In other words, once declared, the variable can be used in any line of codes throughout the rest of the program.
A variable define outside is external. An external variable can also be declared within the function that uses it by using the keyword extern hence it can be accessed by other code in other files.
The Syntax of External Variable is as follows:
   exturn int value1;
   exturn char name;
   exturn double value2;
Static Variable - static  
In a static file program, static variables are defined. Static variables are local to the function in they are defined. Static variables are local variables that retain their values throughout the lifetime of the program. In other words, their same(or the latest) values are still available when the function re-invoked later.
-their values can be utilized within the function in the same manner as other variables, but they cannot be accessed from outside of their defined function.
The Syntax of Static Variable is as follows:
void show(){static int s=0; s++; cout<<;}
Example
Output
Register Variable - register
 The above three classes of variable are normally stored in computer memory. Register variables however are stored in the processor registers, where they can accessed and manipulated faster. Register variables, like automatic variables, are local to the function in which they are declared.
Usually, only register variables are assigned the register storage class. If all things equal, a program that makes use of register variables is likely to run faster than an identical program that uses just automatic variables.
The Syntax of Register Variable is as follows:
    register int a;
Few terms
  • Scope:the scope of a variable determines over what part(s)of the program a variable is actually available for use(active).
  • Lifetime or Longevity:it refers to the period during which a variables retains a given value during execution of aprogram(alive)
  • Local(internal) variables:are those which are declared within a particular function.
  • Global(external) variables:are those which are declared outside any function.