Register Storage Class in C Programming

This is also used for the local variables but used when faster access is required. This type of variable is stored in the CPU registers rather than the RAM. Hence these variables will not have any memory address assigned, and thus we cannot access these variables using the address operator ‘&’. But these variable acts as any other local variable or auto variables. The keyword ‘register’ is used before the variable declaration to indicate that it is register variable. Even though we declare them as register variable, it need not be stored in the registers. CPU will have very limited number of registers and if they are occupied with other tasks and if there is no availability of registers, they are stored as automatic variable. That means it will be stored in the memory.

The main advantage of storing the variable as register is they are stored in CPU memory which is accessed very fast compared to RAM. This makes the program to get executed faster. Hence these type of variables are mainly used where quick access to them are required. For example indexes of loops, while calculating and accessing counters etc.

Since ‘&’ operator cannot be used with these type of variables, we cannot use register type of variable for arrays. This is because array itself acts like a pointer to the memory address and its elements to be stored in contiguous memory locations. But registers will not have any memory allocated and hence it cannot be used for arrays.

Registers are also local variables and hence their scope exists only within the block or the function in which it is defined. Like auto, it will have garbage value till it is initialized. Hence we will not be able to access this variable until we initialize it.

#include 

void main(){
	register int intNum;

	printf("\nValue at intNum before initializing is %d ", intNum);// shows compilation error that intNum is not initialized

	intNum = 100;
	printf("\nValue at intNum after initializing is %d ", intNum);
}
Translate »