The register Keyword in C

Waqar Aslam Oct 12, 2023
  1. an Overview of register Keyword in C Programming Language
  2. Usage of Register Keyword in C
  3. Get the Address of a Register Variable in C
  4. Use the register Keyword With a Pointer Variable in C
  5. Usage of Storage Classes With register Keyword in C
  6. Scope of the register Variable in C Programming Language
The register Keyword in C

This article will discuss the register keyword in the C programming language.

an Overview of register Keyword in C Programming Language

All of the variables in a C program are saved to memory, each with its memory address. However, if you use the register keyword in conjunction with a variable, the compiler is more likely to save the variable in a CPU register than in memory.

In case you were unaware, a processor has a finite number of general purpose registers, and it is the responsibility of the central processing unit (CPU) to load all variables into their respective registers before performing arithmetic or logical operations on data.

Registers are much quicker than memory, and the access timer for the CPU is correspondingly shorter.

As a result, we pair a register keyword with a variable with the highest frequency of occurrence throughout the program. The register keyword tells the compiler that the variable in question should be stored in a processor register so that it may be retrieved in the shortest amount of time possible.

However, it is up to the compiler to decide whether or not to store this variable in the memory or a processor register.

Let us look at multiple scenarios where the register keyword can be used in the programs.

Usage of Register Keyword in C

Below is a simple example of the register keyword used in the C programming language.

#include <stdio.h>

int main() {
  register char a = 'S';
  register double b = 10;
  register int c = 8;
  printf("The value of register variable a: %c\n", a);
  printf("The value of register variable b: %f\n", b);
  printf("The value of register variable c: %d", c);
  return 0;
}

Output:

The value of register variable a: S
The value of register variable b: 10.000000
The value of register variable c: 8

Get the Address of a Register Variable in C

Suppose you place a register keyword with a variable. In that case, you cannot access the address of the variable using the unary address operator & even if the compiler decides to store the variable in memory rather than a CPU register.

This is a crucial fact to keep in mind.

If you combine the address operator & with a register variable, the compiler you are using will either give you a warning or an error. The message that you get will depend on which one it is.

Because when you use the register keyword with a variable, the variable’s value can be placed in a processor register rather than in memory, and processor registers do not have any address. Let us take an example.

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

Output:

main.c: In function ‘main’:
main.c:5:5: error: address of register variable ‘registerVariable’ requested
    5 |     int* ptr = &registerVariable;
      |     ^~~

Use the register Keyword With a Pointer Variable in C

The register keyword may be used with the pointer. It can have the address of a memory location.

Here is an example of the register keyword used with the pointer variable in the C programming language.

#include <stdio.h>

int main() {
  int value = 15;
  register int* pointerVariable = &value;
  printf("The value of the pointer is: %d", *pointerVariable);
  return 0;
}

Output:

The value of the pointer is: 15

Usage of Storage Classes With register Keyword in C

The C programming language does not permit using various storage class specifiers for a single variable. The register is a storage class.

Therefore, you cannot use the register in combination with static, auto, and extern.

#include <stdio.h>

int main() {
  auto register int value = 15;
  printf("The value of the pointer is: %d", value);
  return 0;
}

Output:

main.c: In function ‘main’:
main.c:5:5: error: multiple storage classes in declaration specifiers
    5 |     auto register int value = 15;
      |     ^~~~

Scope of the register Variable in C Programming Language

It is only possible to use the register keyword in conjunction with a local variable; it is not possible to use a register with a variable that has a global scope.

#include <stdio.h>

register int value = 22;
int main() {
  printf("%d", value);
  return 0;
}

Output:

main.c:3:14: error: register name not specified for ‘value’
    3 | register int value = 22;
      |
Author: Waqar Aslam
Waqar Aslam avatar Waqar Aslam avatar

I am Waqar having 5+ years of software engineering experience. I have been in the industry as a javascript web and mobile developer for 3 years working with multiple frameworks such as nodejs, react js, react native, Ionic, and angular js. After which I Switched to flutter mobile development. I have 2 years of experience building android and ios apps with flutter. For the backend, I have experience with rest APIs, Aws, and firebase. I have also written articles related to problem-solving and best practices in C, C++, Javascript, C#, and power shell.

LinkedIn