Register Variables

Summary: Register variables are stored in processor registers instead of addressable memory.

You can use the register keyword in variable declarations to request that a variable be stored in a processor register. Because processor registers can be accessed more quickly than addressable memory locations, this storage can make a program run faster. Programmers use register to speed access to heavily used variables, such as counter variables in loops.

The register specifier is much less important than it used to be, now that most C compilers, including QuickC, can perform optimizations (improvements) during compilation. If you compile with the Optimizations option turned on, QuickC automatically stores variables in registers when needed. So you probably won't need to use register except in special cases.

IMPORTANT:

If you compile with Optimizations on, an explicit register declaration can override register storage that QuickC would do automatically. Declaring one variable with register might prevent QuickC from storing some other variable in a register. In the worst case, this can make a program run slower.

You can use register only with short integer types (char, int, and short int). Other types—including aggregate types such as arrays—are too large to fit in a register.

Only two registers are available for variable storage at any given time. (They are DI and SI, for those who have programmed in assembly language.) If you request more registers than are available, QuickC stores the extra variables in addressable memory, as it does non-register variables.

The following declaration uses register to ask the compiler to store the int variable val in a processor register:

register int val;

You can ask the compiler to store more than one variable in a register. For instance, the statement

register int val, count;

declares val and count as register variables.

NOTE:

Since registers are not addressable, you can't use the address-of (&) operator to get the address of a variable declared with register. This rule applies whether or not QuickC is actually able to store the variable in a register. Thus, if you need to access a variable through a pointer, don't declare that variable with register. For more information, see “Initializing a Pointer Variable”.