Understanding “register” keyword in C
Registers are faster than memory to access, so the variables which are most frequently used in a C program can be put in registers using the register keyword. The keyword register hints to the compiler that a given variable can be put in a register. It’s the compiler’s choice to put it in a register or not. Generally, compilers themselves do optimizations and put the variables in a register.
Following are some interesting facts about the “register” keyword in C:
1. If you use & operator with a register variable then the compiler may give an error or warning (depending upon the compiler you are using), because when we say a variable is a register, it may be stored in a register instead of memory and accessing the address of a register is invalid.
Try the below program:
C
#include <stdio.h>
int main()
{
register int i = 10;
int * a = &i;
printf ( "%d" , *a);
getchar ();
return 0;
}
|
Output
./Solution.c: In function 'main':
./Solution.c:6:5: error: address of register variable 'i' requested
int* a = &i;
^
2. register keyword can be used with pointer variables. Obviously, a register can have the address of a memory location. There would not be any problem with the below program.
C
#include <stdio.h>
int main()
{
int i = 10;
register int * a = &i;
printf ( "%d" , *a);
getchar ();
return 0;
}
|
3. Register is a storage class, and C doesn’t allow multiple storage class specifiers for a variable. So, the register can not be used with static.
Try the below program:
C
#include <stdio.h>
int main()
{
int i = 10;
register static int * a = &i;
printf ( "%d" , *a);
getchar ();
return 0;
}
|
Output
./Solution.c: In function 'main':
./Solution.c:6:5: error: multiple storage classes in declaration specifiers
register static int* a = &i;
^
4. Register can only be used within a block (local), it can not be used in the global scope (outside main).
C
#include <stdio.h>
register int x = 10;
int main()
{
register int i = 10;
printf ( "%d\n" , i);
printf ( "%d" , x);
return 0;
}
|
Output
./Solution.c:4:14: error: register name not specified for 'x'
register int x = 10;
^
5. There is no limit on the number of register variables in a C program, but the point is compiler may put some variables in the register and some not.
Last Updated :
30 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...