Register in C
Reading time: 20 minutes | Coding time: 5 minutes
Register is a keyword in C which suggests the system to use register as a memory for a variable instead of RAM. This accelerates the reading and writing of memory and enhances the overall performance. Note that using register does not gaurentee the use of system registers.
The time of memory access is as follows:
registers < cache < main memory (default) < secondary memory
Syntax:
register <datatype> <variable_name>;
Example:
register int data;
Complete example:
#include <stdio.h> int main() { for(register int i = 1; i <= 5; i++) printf("%d", i); return 0; }
Output:
12345
Currently, most comiplers are optimized so that it is able to decide automatically which variable if stored in register will result in performance improvement.
Note that register is a reserved but unused keyword in C++. This is because C++ designers considered that compilers will be mature enough to tune this parameter better than developers in most cases.
Some key points regarding register in C:
- register only suggests using register memory
- We cannot get the memory location of such a variable
- We can get the size using sizeof()
Memory location
If we have declared a variable as a register, we cannot get the memory location.
Normal way of getting memory location of a variable is using the & operator like:
printf("%p", &i);
Complete example:
#include <stdio.h> int main() { int i = 0; printf("%p", &i); return 0; }
Output:
0x7fff3d4f4934
If i, the variable is declared as a register, it will give compile time error. For example:
#include <stdio.h> int main() { register int i = 0; printf("%p", &i); return 0; }
Compile time error:
opengenus.c: In function 'main': opengenus.c:6:5: error: address of register variable 'i' requested printf("%p", &i); ^
sizeof() of register variable
We can get the size of a register variable using sizeof() just like a normal variable like:
printf("%lu", sizeof(i));
Complete C code example:
#include <stdio.h> int main() { int i = 0; printf("%lu", sizeof(i)); return 0; }
Output:
4
Comparing performance
Consider this code where we are considering registers and executing a loop:
#include <stdio.h> #include <time.h> #define limit 10000000 int main() { clock_t start = clock(); register int j = 0; for(register int i = 1; i <= limit; i++) { j = i * 2 - 1; j = j * j; j = i; } clock_t end = clock(); printf("%ld", (long)(end-start)); return 0; }
Output:
2
It takes only 2 clock cycles. It uses registers.
If we avoid using registers, it will increase the time greatly. Consider this example:
#include <stdio.h> #include <time.h> #define limit 1000000 int main() { clock_t start = clock(); int j = 0; for(int i = 1; i <= limit; i++) { j = i * 2 - 1; j = j * j; j = i; } clock_t end = clock(); printf("%ld", (long)(end-start)); return 0; }
Output:
2714
Hence, we see that the execution time is significantly more when not using registers. Hence, in C programming, it is advised to use register for variables that will be used frequently.
Note that declaring register for all variables may not help as the system may not have so many registers available for use.