C register keyword

In C register keyword suggests to the compiler that the current variable should be stored in a CPU register, rather than memory.

The idea

Advertise on this site. I promise you will like the rates :)

The central processor can hold a very small amount of data. This data is stored in places called registers. Data saved there, is accessed faster than data stored in memory. For that reason programs can load their most-frequently accessed data in the CPU registers.

This is the very idea of the register keyword in C. It tells the compiler that we will work frequently with the current variable and it should be stored for fast access. Note that this is only a “hint” to the compiler and it could ignore it(and most likely it will, but more about this in a minute).

C register – example in code

int values[5];
register int i;
for(i = 0; i < 5; ++i)
{
    values[i] = i;
}

Restrictions

Register is a storage class specifier. We cannot use more than one storage class specifier for one variable.
Attempting to assign more than one storage class specifier will result in an error like "error: multiple storage classes in declaration specifiers"

This means that the following declarations are not correct:


Placing a variable in a processor register means that it does not have a memory address, because it is not in the memory. Therefore, in C, if you declare a variable with the register keyword, you cannot access its address with the & operator.

Even if the variable is placed in memory, the compiler should not allow you to access its address. You can use this to make sure that some variable will not be changed outside of its function.
The following code should not compile. In my case it returns a compile-time error "error: address of register variable ‘number’ requested"

If it compiles, it should give a warning and chances are the program will crash.

1
2
3
4
5
6
7
8
int main()
{
    register int number = 1;
    scanf("%d", &number);
    printf("%d", number);

    return 0;
}

If you use a C++ compiler: Many people are using C++ compilers to compile their C programs. This is OK, but it could cause some confusion, because in C++ it is allowed to access the address of a register variable.

Usage in practice

Hardly any.

Back in the days compilers were not very good at deciding when to use machine registers for variables. For that reason the C developers did this manually with the register keyword.

With time the compilers got smarter. They became better than us in deciding about variables storage. Today it is very unlikely that a developer will make a better decision about a register variable. In fact, most of the time you could only make things slower. That's why many compilers will always ignore the C register keyword when it comes to storage.

However, like we said earlier, you can use the C register keyword to make sure your variable will not be accessed by address and changed outside of the scope that you expect.

See also:

Previous keywordlong

Next keywordreturn

   Search this site: