Loading, please wait...

Register Variables

Data in the machine-language version of a program is normally loaded into registers for calculations and other processing. The storage-class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintains the variable in one of the computer’s high-speed hardware registers. If intensely used variables such as counters or totals can be maintained in hardware registers, the overhead of repeatedly loading the variables from memory into the registers and storing the results back into memory can be eliminated.

The compiler may ignore register declarations. For example, there may not be a sufficient number of registers available for the compiler to use. The following declaration suggests that the integer variable counter is placed in one of the computer’s registers and initialized to 1:

register int counter = 1;

Keyword register can be used only with variables of automatic storage duration. Often, register declarations are unnecessary. Today’s optimizing compilers are capable of recognizing frequently used variables and can decide to place them in registers without the need for a register declaration