In the
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
,
register
is a
reserved word
In a programming language, a reserved word (sometimes known as a reserved identifier) is a word that cannot be used by a programmer as an identifier, such as the name of a variable, function, or label – it is "reserved from use". In brief, an '' ...
(or keyword), type modifier,
storage class
The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively ...
, and hint. The
register
keyword was deprecated in
C++, until it became reserved and unused in
C++17
C17, C-17 or C.17 may refer to:
Transportation
* , a 1917 British C-class submarine
Air
* Boeing C-17 Globemaster III, a military transport aircraft
* Lockheed Y1C-17 Vega, a six-passenger monoplane
* Cierva C.17, a 1928 English experimental ...
. It ''suggests'' that the
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
stores a
declared variable in a
CPU
A central processing unit (CPU), also called a central processor, main processor, or just processor, is the primary processor in a given computer. Its electronic circuitry executes instructions of a computer program, such as arithmetic, log ...
register
Register or registration may refer to:
Arts, entertainment, and media
Music
* Register (music), the relative "height" or range of a note, melody, part, instrument, etc.
* ''Register'', a 2017 album by Travis Miller
* Registration (organ), ...
(or some other faster location) instead of in
random-access memory
Random-access memory (RAM; ) is a form of Computer memory, electronic computer memory that can be read and changed in any order, typically used to store working Data (computing), data and machine code. A random-access memory device allows ...
. If possible depending on the type of
CPU
A central processing unit (CPU), also called a central processor, main processor, or just processor, is the primary processor in a given computer. Its electronic circuitry executes instructions of a computer program, such as arithmetic, log ...
and complexity of the program code, it will optimize access to that variable and 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
sizeof is a unary operator in the C and C++ programming languages that evaluates to the storage size of an expression or a data type, measured in units sized as char. Consequently, the expression sizeof(char) evaluates to 1. The number of b ...
operator can be applied. 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 the programmer to specify a particular register with the keyword; however, this is discouraged in favor of the compiler's optimizations.
When used,
register
is typically for
loop counter
In computer science, a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for-loop functions by running a section of code repeatedly until a certain condition has been satisfied.
For-loops have two parts: ...
s, or possibly for other very frequently used variables in the code.
Examples
/* store integer variable "i" in RAM, register, or other location as compiler sees fit */
int i;
/* suggests storing integer variable "i" in a CPU register or other fast location */
register int i;
See also
*
Optimizing compiler
An optimizing compiler is a compiler designed to generate code that is optimized in aspects such as minimizing program execution time, memory usage, storage size, and power consumption. Optimization is generally implemented as a sequence of op ...
*
Program optimization
In computer science, program optimization, code optimization, or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be op ...
*
Static (keyword)
static is a reserved word in many programming languages to modify a declaration. The effect of the keyword varies depending on the details of the specific programming language, most commonly used to modify the lifetime (as a static variable) and ...
References
{{Reflist
C (programming language)
Variable (computer science)