Javatpoint Logo
Javatpoint Logo

Register Keyword in C

All variables in a C program have a memory address and are stored in memory. However, the register keyword implies that the compiler should store the variable in a CPU register as opposed to memory. If you're unaware, a processor only has a certain number of general-purpose registers, and the CPU loads all variables into those registers prior to performing any arithmetic or logical operations on the data. Registers are significantly faster than memory, and the CPU's access timer is likewise shorter. As a result, we pair a variable that is regularly utilised in the programme with the register keyword. In other words, the register keyword instructs the compiler to save the variable in a processor register to enable quick access to it. However, the compiler decides whether to save this variable in memory or a processor register.

How to use Register Keyword?

You only need to include the register keyword with a variable to turn it become a register variable. You can look at the following examples:

Register keyword examples

The following example sets up various register variables of various types and uses print() to show their values:

Output:

The value of register variable x : B
The value of register variable a : 20
The value of register variable b : 35

Can we get a Register variable's Address?

One thing to keep in mind is that even if the compiler chooses to store the variable in memory rather than a CPU register, if you include the register keyword with a variable, you cannot access its address using the unary address operator (&).

Depending on your compiler, using the address operator (&) with a register variable will result in either a warning or an error because CPU registers have no addresses and may be used to store variables instead of memory when the register keyword is used. Check out the following example code and its results for a demonstration:

C Example to access register variable address

Output:

In this instance, our compiler generates an error. Some compilers, however, just issue warnings.

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

With pointer variables, we can use the register keyword similarly to other data types. In this instance, it will have a memory location address.

Output:

22

Scope of register Variable

Only local variables may be used with the register keyword; global variables may not be used with this keyword.

Output:

main.c:3:14: error: register name not specified for 'x'
 register int x = 22;

Important information regarding register keyword

First of all, the C standard forbids using more than one storage specifier with a single variable. The register keyword is also a storage class in C. In C, there are four different kinds of storage class specifiers:

  • extern
  • auto
  • static
  • register

Because of this, we are unable to utilize register with the extern auto and static storage class specifiers.

Output:

main.c: In function 'main':
main.c:5:5: error: multiple storage classes in declaration specifiers
     static register int x = 22;






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA