Mastering Register Storage Class: A Step-by-Step Guide
Register Storage Class
This storage class is same as the automatic storage class, except that the variable is not stored on computer’s memory. The register variable is stored on the CPU registers. The way we can use this class is:
register int num;
Here, ‘num’ is the variable which is declared as belonging to ‘the ‘register’ storage class. Even tough we declare a variable with the key word ‘register’, the variable may or may not be stored actually on the CPU registers. The reason is there is limited number of CPU registers.CPU registers are part of CPU and can be accessed immediately by the processor. Hence, if the variable value is stored in the CPU registers, it can be accessed faster than any other variable.Generally, the variables which are most often used such as the loop counters are declared as of ‘register’ class.
Program to understand usage of register variable
#include<stdio.h>
void main()
{
register int i;
for(i=0; i<10; i++)
printf("\n%d", i);
}
Output:
0
1
2
3
4
5
6
7
8
9
In the above code, since i is used as loop counter, it is declared as of ‘register’ storage class. Generally, CPU registers can store only 16 bits(2 bytes).So, it is advisable to use register key word only for integer type variable.