Register storage class - Time & Space Complexity
Let's see how using the register storage class affects how fast a program runs.
We want to know if it changes how the program's speed grows when input gets bigger.
Analyze the time complexity of the following code snippet.
void countToN(int n) {
register int i;
for (i = 0; i < n; i++) {
// do something simple
}
}
This code counts from 0 up to n-1 using a register variable for the loop counter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs a simple operation inside.
- How many times: It runs exactly n times, where n is the input size.
As n grows, the loop runs more times, so the total work grows in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows evenly as n grows; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the input size.
[X] Wrong: "Using register makes the loop run faster and changes the time complexity."
[OK] Correct: Register may speed up access but does not change how many times the loop runs, so the overall growth stays the same.
Understanding how storage classes affect performance helps you explain code efficiency clearly and confidently.
"What if we replaced the register variable with a normal variable? How would the time complexity change?"
Practice
register storage class in C?Solution
Step 1: Understand the role of
Theregisterkeywordregisterkeyword suggests to the compiler that the variable should be stored in CPU registers for faster access.Step 2: Compare with other storage classes
Unlike global or heap variables,registervariables are local and optimized for speed, not for scope or memory allocation.Final Answer:
To suggest storing the variable in CPU registers for faster access -> Option AQuick Check:
registermeans fast access [OK]
- Thinking register allocates memory on the heap
- Assuming register variables are global
- Believing register variables can be accessed outside functions
Solution
Step 1: Recall correct syntax for register declaration
The correct syntax places the keywordregisterbefore the type:register int count;.Step 2: Check other options for syntax errors
Options A, B, and C have incorrect order or invalid assignment syntax.Final Answer:
register int count; -> Option DQuick Check:
Keyword before type = correct syntax [OK]
- Placing 'register' after the variable name
- Using assignment '=' in declaration
- Mixing order of type and storage class
void func() {
register int x = 5;
// printf("%p", &x); // Uncommenting this line causes error
printf("%d", x);
}
int main() {
func();
return 0;
}Solution
Step 1: Understand register variable usage
The variablexis declared asregister int, so it cannot have its address taken.Step 2: Analyze the code behavior
The commented line with&xwould cause a compilation error if uncommented. But as is, the code prints the value ofx, which is 5.Final Answer:
5 -> Option CQuick Check:
Printing register variable value works fine [OK]
- Trying to print address of register variable
- Expecting runtime error instead of compile error
- Confusing garbage value with initialized value
void example() {
register int a = 10;
int *p = &a;
printf("%d", *p);
}Solution
Step 1: Check usage of register variable
The variableais declared asregister int, so its address cannot be taken.Step 2: Identify the error in pointer assignment
The lineint *p = &a;tries to take the address of a register variable, which causes a compilation error.Final Answer:
Cannot take address of register variable -> Option BQuick Check:
Address of register variable forbidden [OK]
- Thinking register variables can't be initialized
- Assuming pointer declaration is wrong
- Believing code runs without error
void count() {
register int i;
for(i = 0; i < 3; i++) {
printf("%d ", i);
}
}
int main() {
count();
return 0;
}What is the main advantage of using
register for variable i here?Solution
Step 1: Analyze the role of
Loop variables likeregisterin loopsiare used repeatedly, so storing them in CPU registers speeds up access.Step 2: Check other options for correctness
It allows taking the address ofiinside the loop is wrong because you cannot take the address of a register variable. It makesia global variable is wrong becauseiis local, not global. It automatically initializesito zero is wrong becauseregisterdoes not initialize variables.Final Answer:
It suggests faster access toisince it is frequently used -> Option AQuick Check:
Register = fast access for frequent vars [OK]
- Thinking register allows & operator
- Assuming register makes variable global
- Believing register initializes variables automatically
Ready to prove it?
Pass the test and this pattern moves unknown
