0
0
Cprogramming~10 mins

Register storage class - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Register storage class
Declare variable with 'register'
Compiler tries to store variable in CPU register
Variable used in code
Fast access to variable
End of scope: variable destroyed
The 'register' keyword suggests the compiler to store the variable in CPU register for faster access during its scope.
Execution Sample
C
#include <stdio.h>

void func() {
  register int count = 5;
  count = count + 1;
  printf("%d", count);
}
This code declares a register variable 'count', increments it, and prints the result.
Execution Table
StepActionVariable 'count' ValueNote
1Declare 'count' as register int with value 55Compiler tries to store 'count' in CPU register
2Increment 'count' by 16'count' updated to 6
3Print 'count'6Output: 6
4End of function scopeN/A'count' destroyed, register freed
💡 Function ends, 'count' goes out of scope and register is freed
Variable Tracker
VariableStartAfter Step 2After Step 3Final
count566Destroyed
Key Moments - 2 Insights
Does 'register' guarantee the variable is stored in CPU register?
No, 'register' is a hint to the compiler. The compiler may ignore it if registers are limited, as shown in Step 1 of the execution_table.
Can we get the address of a 'register' variable using '&' operator?
No, you cannot take the address of a 'register' variable because it may not have a memory address, as it might be stored in a CPU register.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after Step 2?
A6
B5
C7
DUndefined
💡 Hint
Check the 'Variable 'count' Value' column in row for Step 2 in execution_table.
At which step does the variable 'count' get destroyed?
AStep 2
BStep 3
CStep 4
DNever
💡 Hint
Look at the 'Note' column in execution_table for when the function scope ends.
If we remove 'register' keyword, how would the execution_table change?
AVariable 'count' would not be initialized
BVariable 'count' would be stored in memory, but values and output remain same
COutput would change to 5
DVariable 'count' would not be destroyed
💡 Hint
Removing 'register' affects storage location, not the logic or output, as seen in variable_tracker.
Concept Snapshot
register storage class in C:
- Suggests compiler to store variable in CPU register
- Used for fast access to frequently used variables
- Cannot take address of register variables
- Compiler may ignore this hint
- Variable lifetime is limited to its scope
Full Transcript
This visual trace shows how a variable declared with the 'register' storage class behaves in C. The variable 'count' is declared with 'register' and initialized to 5. The compiler tries to store it in a CPU register for faster access. Then 'count' is incremented to 6 and printed. Finally, when the function ends, 'count' is destroyed and the register is freed. Note that 'register' is only a suggestion to the compiler and does not guarantee register storage. Also, you cannot take the address of a register variable. Removing 'register' changes storage location but not the program output.