register Storage Class
The register storage class declares register variables using the ‘register’ keyword which has the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available, these are then stored in the memory only.
An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.
Properties of register Storage Class Objects
- Scope: Local
- Default Value: Garbage Value
- Memory Location: Register in CPU or RAM
- Lifetime: Till the end of its scope
Example of register Storage Class
C++
// C++ Program to illustrate the use of register variables #include <iostream> using namespace std; void registerStorageClass() { cout << "Demonstrating register class\n" ; // declaring a register variable register char b = 'G' ; // printing the register variable 'b' cout << "Value of the variable 'b'" << " declared as register: " << b; } int main() { // To demonstrate register Storage Class registerStorageClass(); return 0; } |
Demonstrating register class Value of the variable 'b' declared as register: G
Note: The register keyword is deprecated in C++17 onwards.
Storage Classes in C++ with Examples
C++ Storage Classes are used to describe the characteristics of a variable/function. It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specify the storage class for a variable.
Syntax
To specify the storage class for a variable, the following syntax is to be followed:
storage_class var_data_type var_name;
C++ uses 6 storage classes, which are as follows:
- auto Storage Class
- register Storage Class
- extern Storage Class
- static Storage Class
- mutable Storage Class
- thread_local Storage Class
Below is a detailed explanation of each storage class:
Contact Us