• Home
  • 1.9 register Storage Class

1.9 register Storage Class

View Categories

1.9 register Storage Class

< 1 min read

The register storage-class specifier can be used for an automatic variable to request that the implementation keep the variable in a processor register when possible. The compiler is not required to honor the request.

#include <stdio.h>

int main()
{
    register int counter = 0;

    while (counter < 5)
    {
        printf("%d\n", counter);
        counter++;
    }

    return 0;
}

Example Output #

0
1
2
3
4

Explanation #

The variable is declared with register:

register int counter = 0;

This gives the implementation a request to use register storage for counter. Modern optimizing compilers generally perform their own register allocation, so declaring a variable as register does not guarantee that it will actually be stored in a CPU register.

A register variable is subject to an important restriction: its address cannot be obtained using the address-of operator &.

register int counter = 0;

/* Invalid */
printf("%p", (void *)&counter);

The register specifier does not change the variable’s type or value range. It specifies a storage-class property of the variable.

register Reference Table #

Property register variable
Storage class register
Typical scope Block scope
Storage duration Automatic
Register storage guaranteed No
Address can be taken with & No
Type changed by register No

register is primarily a language-level storage request; the actual storage decision is made by the implementation.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *