The previous tutorial covered several features of the auto storage class, including how to define an automatic variable. This tutorial will teach you how to define a variable as a register variable and the advantages of doing so. So, let’s dive in.
The Syntax
The syntax to declare a register variable is as follows:
register data_type variable_name;
Example:
register int var = 10;
Features of a Register Storage Class
Following are the features of a register storage class:
Storage: register.
Default value: garbage value.
Scope: local to the block in which it is defined.
Lifetime: till the vicinity of the block in which it is defined.
Need of Register Storage Class
A register variable is a variable that is stored in a special type of memory called a register. Registers are much faster than RAM (Random Access Memory), so defining frequently used variables as register variables can speed up the processing of a program. For example, loop counters can be defined as register variables because they are used multiple times in a program. Here is a program to better understand this:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
Output:
If you are new to the for loop in a program, please refer to the following image to understand how it works:
Flow of Execution of a for Loop
A for loop is used to execute a block of code repeatedly, as many times as specified by a condition. In the example program, the for loop repeats the printf function 10 times. Let’s see how many times the variable i is referenced inside the for loop.
First, i is initialized to zero. Then, it is compared to the value 10. Since 0 is less than 10, the condition is true and control is transferred to the printf function, which prints the value 0 on the screen. After this, the variable i is incremented by 1 using i++ (i++ is equivalent to i = i + 1). This means that in the first iteration, the variable i is used a total of 4 times.
Next, i is again compared to the value 10 (note that i is initialized only once at the beginning). Since the comparison returns true, control is transferred to the printf function inside the for loop. This time, 1 is printed and i is again incremented by 1. This time, the variable i is used a total of 3 times. The rest of the process will continue the same until i becomes 10, at which point the condition i < 10 becomes false and control goes outside the for loop.
So, the variable i is used a total of 4 + 9 * 3 = 31 times, which clearly shows the need of storing it as a register variable.
Register variables are stored in the CPU’s registers, which are much faster than main memory. This means that storing i as a register variable will make the program run faster.
It is important to note that adding the keyword “register” before a variable does not always guarantee that the variable will be stored in a CPU register. This is because there are a limited number of registers in a computer, and they may be already in use for other tasks. In such a case, the variable i will be stored in memory as if its storage class is auto.
Leave a comment