register Keyword in C

In a C program, all variables store in memory and have a memory address. But using register keyword with a variable suggests compiler to store this variable in a CPU register rather than in memory. If you don’t know, there are limited number of general purpose registers in a processor and CPU loads all variables into their registers before before arithmetic and logical operations on data. Registers are very faster than memory and access timer is also less for CPU. Therefore, we use a register keyword with a variable which is most frequently used in the program. In other words, register keyword gives a suggestion to compiler to place that variable in a processor register so that it can be accessed within minimum time. But it is up to compiler to put this variable in a processor register or in memory.

Note: All modern compilers perform this optimization automatically and put most frequently used variable in a processor register.

How to use Register Keyword?

To make any variable as a register variable, you just need to put register keyword with a variable. You can check these examples:

register char a = 'Z'; //character type register variable
register int x = 10; //integer type register variable
register short z = 22; //short type register variable

The following example initialize different types of register variables and display its values with print():

#include <stdio.h>
int main() {
   register char x = 'A';
   register int a = 10;
   register short b = 53;
   printf("The value of register variable x : %c\n",x);
   printf("The value of register variable a : %d\n",a);
   printf("The value of register variable b : %d\n",b);
   return 0;
}

Output:

The value of register variable x : A
The value of register variable a : 10
The value of register variable b : 53

Can We Get Address of a Register Variable?

One important point to note here is that if you put a register keyword with a variable, you can not get its address with unary address operator (&), even if compiler decide to put it in memory instead of a processor register.

If you use a address operator (&) with a register variable, you will get either warning or error depending on the compiler which you are using. Because when you use register keyword with a variable, it may get stored in a processor register instead of memory and processor registers do not have any address. For demo check the example code and its output given below:

#include <stdio.h>
int main()
{
    register int x = 22; // register variable x
    int* ptr = &x;   // pointer to a variable x
    printf("%d", *ptr); // trying to access value of x with pointer ptr
    return 0;
}

Output:

In our case, our compiler gives us error. But some compilers only give warning.

main.c: In function 'main':
main.c:5:5: error: address of register variable 'x' requested
     int* ptr = &x;   // pointer to a variable x
     ^~~

Register keyword with a Pointer Variable

Similar to other data type, we can also use register keyword with pointer variables. In this case, it will have a address of a memory location.

#include<stdio.h>
  
int main()
{
    int x = 22;
    register int* ptr = &x;
    printf("%d", *ptr);
    return 0;
}

Output:

22

Scope of register Variable

The register keyword can only be used with a local variable and it can not be used with global scope variable.

#include<stdio.h>

register int x = 22;
int main()
{
    printf("%d", x);
    return 0;
}
main.c:3:14: error: register name not specified for 'x'
 register int x = 22;
              ^

Important points about register Keyword

Firslty, register keyword is also a storage class in C and C standard does not allow multiple storage specifiers with a single variable. There are four types of storage class specifiers in C:

  • extern
  • auto
  • static
  • register

Hence, we can not use register with extern auto and static storage class specifiers.

#include<stdio.h>
  
int main()
{
    static register int x = 22;
    printf("%d", x);
    return 0;
}
main.c: In function 'main':
main.c:5:5: error: multiple storage classes in declaration specifiers
     static register int x = 22;
     ^~~~~~

Leave a Comment