Storage Classes in C++
Storage Classes in C++
Storage classes in C++ is used to define and describe the lifetime and visibility or features of a variable and function. This features contains the scope, visibility which help us to find the existence of a particular variable during the runtime of a program
Syntax of storage class
storage_class var_data_type var_name;
There are five types of storage classes, they are follows
- Automatic
- Register
- Static
- External
- Mutable
1. Auto Storage Class in C++
Automatic storage class is the default storage class for all local variables, which are declared inside a block. Keyword auto is used for declaring automatic variables.
Syntax of auto strorage class in C++
auto datatype var_name1 [= value];
Program of auto storage class in C++
#include <iostream> using namespace std; void autoStorageClass() { cout << "Auto class\n"; auto a = 56; auto b = 5.6; auto c = "StudyExperts"; auto d = 'S'; cout << a << " \n"; cout << b << " \n"; cout << c << " \n"; cout << d << " \n"; } int main() { autoStorageClass(); return 0; }
Output
Auto class 56 5.6 StudyExperts S
2. Register Storage Class
register keyword is used for specifying register variables.
Register variables are similar to auto variables that is having same functionality and exists inside a particular function only. It is supposed to be faster than the variables stored in memory during runtime program
Program of register storage class in C++
#include <iostream> using namespace std; void registerStorageClass() { cout << " Register class\n"; register char b = 'G'; cout << "Value of the variable 'b'" << " declared as register: " << b; } int main() { registerStorageClass(); return 0; }
Output
Register class Value of the variable 'b' declared as register: G
3. Static Storage Class
static keyword is used to declare a static variable.
The static variable is initialized only one time and exists till the end of a program. It kepp its value between multiple functions call.
The static variable has the default value zero (0) which is provided by compiler.