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.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

The idea

    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. This means that the following declarations are not correct:

    register static int number;
    register auto int number;
    register extern int number;

    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. 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.

See also:

Previous keywordlong

Next keywordreturn

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› register

Did this help? Support me with your vote ;-)

Did this help?