You simply have to specify the register keyword alongside a variable to designate it as a register variable. Below are some instances to refer to:
Register keyword 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
Setting up different register variables of diverse types and displaying their values using the print function is demonstrated in the following example:
#include <stdio.h>
int main() {
register char x = 'B';
register int a = 20;
register short b = 35;
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 : B
The value of register variable a : 20
The value of register variable b : 35
Can we get a Register variable's Address?
It's important to note that in cases where the compiler decides to store a variable in memory instead of a CPU register, the use of the register keyword with that variable will prevent you from retrieving its memory address using the unary address operator (&).
Depending on the compiler you are using, employing the address operator (&) with a register variable can lead to a warning or an error. This is due to the fact that CPU registers do not have addresses and may be designated to store variables instead of in memory when the register keyword is utilized. Below is an example code snippet along with its corresponding outcomes to illustrate this concept:
C Example to access register variable address
#include <stdio.h>
int main()
{
register int x = 22; //x is a register variable.
int* ptr = &x; // pointer to the variable x
printf("%d", *ptr); // attempting to retrieve the value of x with a pointer ptr
return 0;
}
Output:
In this case, our compiler encounters an error. Alternatively, certain compilers merely provide 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
By utilizing pointer variables, we can employ the register keyword in a manner consistent with other data types, where it signifies a specific memory location address.
#include<stdio.h>
int main()
{
int x = 22;
register int* ptr = &x;
printf("%d", *ptr);
return 0;
}
Output:
Scope of register Variable
Only variables defined within a specific function scope are permitted to be utilized with the register keyword; variables declared globally are restricted from being used in conjunction with this keyword.
#include<stdio.h>
register int x = 22;
int main()
{
printf("%d", x);
return 0;
}
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
Due to this limitation, we cannot use register with the extern, auto, and static storage class specifiers.
#include<stdio.h>
int main()
{
static register int x = 22;
printf("%d", x);
return 0;
}
Output:
main.c: In function 'main':
main.c:5:5: error: multiple storage classes in declaration specifiers
static register int x = 22;