Why storage classes are needed - Performance Analysis
We want to see how the use of storage classes affects the time it takes for a program to run.
Specifically, we ask: does choosing different storage classes change how fast the program runs as input grows?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
void countUp(int n) {
static int counter = 0;
for (int i = 0; i < n; i++) {
counter++;
}
printf("Counter: %d\n", counter);
}
int main() {
countUp(5);
countUp(3);
return 0;
}
This code uses a static storage class variable inside a function to keep track of a count across calls.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 0 to n-1.
- How many times: Exactly n times each call to countUp.
As n grows, the number of loop steps grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop steps |
| 100 | 100 loop steps |
| 1000 | 1000 loop steps |
Pattern observation: The work grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to run grows directly with the size of the input n.
[X] Wrong: "Using static variables makes the loop run faster because the variable keeps its value."
[OK] Correct: The static variable only keeps its value between calls; it does not reduce the number of loop steps. The loop still runs n times each call.
Understanding how storage classes affect program behavior helps you explain how variables live and change over time, which is a key skill in writing efficient and clear code.
"What if we changed the static variable to a regular local variable? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of storage classes
Storage classes in C define how long a variable exists and where it can be accessed.Step 2: Differentiate from other concepts
Data types define variable type, arithmetic operations perform calculations, and dynamic memory allocation is separate from storage classes.Final Answer:
To control the lifetime and visibility of variables -> Option DQuick Check:
Storage classes = control lifetime and visibility [OK]
- Confusing storage classes with data types
- Thinking storage classes allocate memory dynamically
- Assuming storage classes perform operations
static storage class in C?Solution
Step 1: Recall correct order of storage class declaration
In C, the storage class specifier comes before the data type.Step 2: Verify each option's syntax
static int count; uses 'static int count;' which is correct. Other options place 'static' incorrectly.Final Answer:
static int count; -> Option BQuick Check:
Storage class before type = correct syntax [OK]
- Placing storage class after variable name
- Mixing order of keywords
- Using invalid syntax like 'count static int;'
void func() {
static int count = 0;
count++;
printf("%d ", count);
}
int main() {
for(int i = 0; i < 3; i++) {
func();
}
return 0;
}Solution
Step 1: Understand static variable behavior inside function
The static variable 'count' retains its value between function calls and initializes only once.Step 2: Trace the loop and function calls
Each call to func() increments count: first call prints 1, second 2, third 3.Final Answer:
1 2 3 -> Option AQuick Check:
Static variable increments across calls = 1 2 3 [OK]
- Assuming static variable resets each call
- Confusing static with automatic variables
- Ignoring initialization behavior
extern int x = 10;
int main() {
printf("%d", x);
return 0;
}Solution
Step 1: Understand 'extern' storage class rules
An 'extern' variable declaration cannot have an initializer; it only declares the variable.Step 2: Analyze the code
Here, 'extern int x = 10;' tries to initialize 'x' which is invalid and causes a compilation error.Final Answer:
'extern' variable cannot be initialized -> Option CQuick Check:
Extern declarations must not initialize variables [OK]
- Initializing extern variables
- Assuming extern means static
- Declaring variables only inside main
Solution
Step 1: Understand variable lifetime and scope requirements
The variable must keep its value between calls (persistent lifetime) and be local to the function (limited scope).Step 2: Match storage classes to requirements
'static' inside a function keeps the variable value between calls and restricts scope to that function.Final Answer:
static -> Option AQuick Check:
Static inside function = persistent and local [OK]
- Using auto which resets each call
- Using extern which is global
- Using register which is for CPU registers
Ready to prove it?
Pass the test and this pattern moves unknown
