Register (keyword)

From Wikipedia, the free encyclopedia
  (Redirected from Register (C programming language))
Jump to navigation Jump to search

In the C programming language and C++, register (not capitalized) is a reserved word (or keyword), type modifier, storage class, and hint. It suggests that the compiler store a declared variable in a CPU register (or some other faster location) instead of in RAM. If possible, depending of type of CPU and complexity of the program code, it will optimize access to that variable, and will hence improve the execution time of a program. In C (but not C++ where the keyword is essentially ignored) the location of a variable declared with register cannot be accessed, but the sizeof operator can be applied.[1] Aside from this limitation, register is essentially meaningless in modern compilers due to optimization which will place variables in a register if appropriate regardless of whether the hint is given. For programming of embedded systems register may still be significant; for example the Microchip MPLAB XC32 compiler allows you to specify a particular register with the keyword, however this is discouraged in favor of the compiler's optimizations.[2] When used, register is typically for loop counters, or possibly for other very frequently used variables in the code.

Examples[edit]

int i; /* will store an integer variable "i" in RAM, register or other location as compiler sees fit and based on how variable is used */

register int i; /* suggests to store an integer variable "i" in a CPU register or other fast location */

See also[edit]

References[edit]