C Language Storage Classes register


Hints to the compiler that access to an object should be as fast as possible. Whether the compiler actually uses the hint is implementation-defined; it may simply treat it as equivalent to auto.

The only property that is definitively different for all objects that are declared with register is that they cannot have their address computed. Thereby register can be a good tool to ensure certain optimizations:

register size_t size = 467;

is an object that can never alias because no code can pass its address to another function where it might be changed unexpectedly.

This property also implies that an array

register int array[5];

cannot decay into a pointer to its first element (i.e. array turning into &array[0]). This means that the elements of such an array cannot be accessed and the array itself cannot be passed to a function.

In fact, the only legal usage of an array declared with a register storage class is the sizeof operator; any other operator would require the address of the first element of the array. For that reason, arrays generally should not be declared with the register keyword since it makes them useless for anything other than size computation of the entire array, which can be done just as easily without the register keyword.

The register storage class is more appropriate for variables that are defined inside a block and are accessed with high frequency. For example,

/* prints the sum of the first 5 integers*/
/* code assumed to be part of a function body*/ 
    register int k, sum;
    for(k = 1, sum = 0; k < 6; sum += k, k++);

The _Alignof operator is also allowed to be used with register arrays.