Static storage class - Time & Space Complexity
Let's see how the static storage class affects the time complexity of a program.
We want to know if using static variables changes how long the program takes as input grows.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
void countCalls() {
static int count = 0;
count++;
printf("Called %d times\n", count);
}
int main() {
for (int i = 0; i < 5; i++) {
countCalls();
}
return 0;
}
This code counts how many times the function countCalls is called using a static variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop inmaincallscountCallsrepeatedly. - How many times: The loop runs 5 times, so the function is called 5 times.
As the number of loop iterations grows, the function calls grow at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to countCalls |
| 100 | 100 calls to countCalls |
| 1000 | 1000 calls to countCalls |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of times the function is called.
[X] Wrong: "Using a static variable makes the function run faster or slower depending on input size."
[OK] Correct: The static variable only keeps its value between calls; it does not change how many times the function runs or how long each call takes.
Understanding how static variables behave helps you explain function behavior clearly and shows you know how storage affects program flow.
"What if the static variable was replaced with a normal local variable? How would the time complexity change?"
Practice
static keyword do when used with a variable inside a function in C?Solution
Step 1: Understand static inside a function
A static variable inside a function keeps its value even after the function finishes.Step 2: Compare with other options
Static does not make variables global or accessible outside the file; it preserves value only inside the function.Final Answer:
Keeps the variable value between function calls -> Option AQuick Check:
Static inside function = value preserved [OK]
- Thinking static makes variable global
- Assuming static deletes variable after function ends
- Confusing static with extern keyword
Solution
Step 1: Recall correct static syntax
The correct syntax placesstaticbefore the type:static int count = 0;.Step 2: Check other options
Other options have wrong order of keywords, which is invalid in C.Final Answer:
static int count = 0; -> Option BQuick Check:
Static keyword comes before type [OK]
- Placing static after the variable name
- Mixing order of keywords
- Using invalid syntax like 'static count int'
void func() {
static int x = 0;
x++;
printf("%d ", x);
}
int main() {
for(int i = 0; i < 3; i++) {
func();
}
return 0;
}Solution
Step 1: Understand static variable behavior in func()
The static variablexis initialized once to 0 and keeps its value between calls.Step 2: Trace the loop calls
Each call incrementsxby 1 and prints it. So outputs are 1, then 2, then 3.Final Answer:
1 2 3 -> Option CQuick Check:
Static variable increments across calls [OK]
- Assuming static resets each call
- Confusing initial value with printed value
- Expecting output to be zeros
void func() {
static int x;
}
int main() {
x = 5;
func();
return 0;
}Solution
Step 1: Check variable scope
The static variablexis declared insidefunc(), so it is local to that function.Step 2: Analyze assignment in main()
Trying to assignx = 5;inmain()causes an error becausexis not visible outsidefunc().Final Answer:
Static variable x cannot be assigned outside its function -> Option DQuick Check:
Static inside function = local scope [OK]
- Thinking static variables are global
- Ignoring variable scope rules
- Assuming static variables must be initialized
In file1.c:
static int count = 0;
void increment() {
count++;
}
int getCount() {
return count;
}In file2.c:
extern int count;
int main() {
increment();
printf("%d", getCount());
return 0;
}What will happen when you compile and run these files together?
Solution
Step 1: Understand static at file scope
Thestatic int counthas internal linkage, visible only in file1.c.Step 2: Function access
increment()andgetCount()are external functions that can access the staticcountsince they are in the same file.Step 3: Analyze extern usage
The declarationextern int count;in file2.c expects a global variablecountdefined elsewhere, but sincecountis static in file1.c, it is not visible externally, causing a linker error.Final Answer:
Compilation error due to undefined reference to count -> Option AQuick Check:
Static variables have internal linkage, extern cannot access them [OK]
- Assuming static variables are global across files
- Expecting extern to find static variables
- Ignoring linker errors
Ready to prove it?
Pass the test and this pattern moves unknown
