In this tutorial you will learn about the C++ Storage Classes and its application with practical example.
C++ Storage Classes
In C++, Storage Classes refers to the scope or visibility and the life time of the C++ Variable. Scope of the C++ Variable defines the availability of the variable in block of the C++ Program, and by life time of variable means how long variable will persist in the program.
Functions of storage class
- It tell the location of the C++ Variable.
- Sets the initial or default value of C++ Variable.
- It defines the scope of the C++ Variable.
- It defines the life time of C++ Variable.
Types of Storage Classes
In C++, we have following four type of storage classes –
- Automatic(auto)
- Register(register)
- Static(static)
- External(extern)
Storage Class | Keyword | Lifetime | Visibility | Initial Value |
---|---|---|---|---|
Automatic | auto | Function Block | Local | Garbage |
Register | register | Function Block | Local | Garbage |
Static | static | Whole Program | Local | Zero |
External | extern | Whole Program | Global | Zero |
Automatic Storage Class
It is the default storage class for local variables. Variables with auto storage class are declared at the beginning of a code block, and memory is allocated automatically as the program execution enters to a code block and frees up automatically on exiting from the code block. The scope of automatic variables is local to the block where the declared and are not accessible directly in the other block. Variable with automatic storage class can be declared using the auto keyword as following –
Syntax:
1 |
auto [data_type] [variable_name]; |
Example :
1 |
auto int a; |
Register Storage Class
In C++, variables with register storage class are same as local variables to the code block but the are stored in CPU register instead of computer memory. Hence it enables the quick access of that variable. Maximum size of the variable is equal to register size and dependent upon the register size. Variable with register storage class can be declared using the register keyword as following –
Syntax:
1 |
register [data_type] [variable_name]; |
Example:
1 |
register int a; |
Static Storage Class
It is default storage class for global variables.Static storage class can be used only if we want the value of a variable to persist between different function calls. In C++, variable declared with static storage class will keep its value retained for different function calls. Variable with static storage class can be declared using the static keyword as following –
Syntax:
1 |
static [data_type] [variable_name]; |
Example:
1 |
static int a; |
Example Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; void func() { static int i=0; //static variable int j=0; //local variable i++; j++; cout<<"i=" << i<<" and j=" <<j<<endl; } int main() { func(); func(); func(); } |
When we run the above C++ program, we will see the following output –
Output:-
1 2 3 |
i= 1 and j= 1 i= 2 and j= 1 i= 3 and j= 1 |
External Storage Class
In C++, variable declared with extern storage class is said to be “Global Variables”, it means variables is accessible throughout the program till the end of program execution. External variables are declared outside the functions and can be invoked from and anywhere in a program. External variables can be accessed very fast as compared to any other storage classes. Variable with external storage class can be declared using the extern keyword as following –
Syntax:
1 |
extern [data_type] [variable_name]; |
Example:
1 |
extern int a; |