Scope of variables - Time & Space Complexity
Let's see how the time it takes to run code changes when we use variables in different places.
We want to know if where a variable is declared affects how long the program runs.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
void example(int n) {
int i;
for (i = 0; i < n; i++) {
int temp = i * 2;
printf("%d\n", temp);
}
}
This code prints double the value of numbers from 0 to n-1, declaring a variable inside the loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs from 0 to n-1.
- How many times: The loop runs n times, and inside each loop, the variable
tempis declared and used.
Each time n grows, the loop runs more times, doing the same steps inside.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times the loop runs |
| 100 | About 100 times the loop runs |
| 1000 | About 1000 times the loop runs |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time grows in a straight line with the input size n.
[X] Wrong: "Declaring a variable inside the loop makes the program slower because it creates the variable every time."
[OK] Correct: In C, declaring a variable inside the loop does not add extra time for creation each time; it just allocates space on the stack each iteration, which is part of the loop's work already counted.
Understanding how variable placement relates to time helps you write clear code without worrying about hidden slowdowns.
"What if we moved the variable temp declaration outside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand variable declaration inside a function
A variable declared inside a function is created when the function starts and destroyed when it ends.Step 2: Identify the scope of such variables
These variables are local to the function and cannot be accessed outside it.Final Answer:
It is local to that function and cannot be accessed outside. -> Option CQuick Check:
Local variable scope = inside function only [OK]
- Thinking local variables are global
- Assuming local variables exist outside function
- Confusing local with static variables
Solution
Step 1: Identify global variable declaration
Global variables are declared outside all functions, usually at the top of the file.Step 2: Check each option
int x; // declared outside any function declaresint x;outside any function, which is correct for global scope.Final Answer:
int x; // declared outside any function -> Option AQuick Check:
Global variables declared outside functions [OK]
- Declaring globals inside functions
- Using static inside functions for globals
- Declaring variables inside loops thinking they are global
int x = 5;
void func() {
int x = 10;
printf("%d", x);
}
int main() {
func();
printf(" %d", x);
return 0;
}Solution
Step 1: Analyze variable scopes in func()
Inside func(), a local variablexis declared with value 10, which hides the globalx.Step 2: Check printf outputs
First printf prints localx(10). Then main prints globalx(5).Final Answer:
10 5 -> Option DQuick Check:
Local hides global inside function [OK]
- Assuming global x prints inside func()
- Mixing order of printed values
- Ignoring local variable shadowing
void func() {
printf("%d", x);
}
int main() {
int x = 5;
func();
return 0;
}Solution
Step 1: Check variable x usage in func()
func() tries to printxbutxis declared only inside main(), so func() cannot see it.Step 2: Understand variable scope rules
Variables declared inside main() are local to main() only, not visible in other functions.Final Answer:
x is not declared globally, so func() cannot access it -> Option AQuick Check:
Local variables not visible outside their function [OK]
- Thinking main()'s x is global
- Ignoring variable scope rules
- Assuming implicit global variables
int x = 1;
void outer() {
int x = 2;
{
int x = 3;
printf("%d ", x);
}
printf("%d ", x);
}
int main() {
outer();
printf("%d", x);
return 0;
}What is the output when this program runs?
Solution
Step 1: Analyze innermost block variable
The innermost block declaresx = 3, so first printf prints 3.Step 2: Analyze outer block variable
After inner block ends, thex = 3goes out of scope, so next printf prints outerx = 2.Step 3: Analyze global variable in main()
Finally, main prints globalx = 1.Final Answer:
3 2 1 -> Option BQuick Check:
Inner blocks hide outer variables [OK]
- Ignoring block-level scope
- Assuming all x print same value
- Confusing order of prints
Ready to prove it?
Pass the test and this pattern moves unknown
