Extern storage class - Time & Space Complexity
Let's explore how using the extern storage class affects the time complexity of a program.
We want to see how the program's running time changes when accessing variables declared with extern.
Analyze the time complexity of the following code snippet.
// file1.c
int count = 0;
void increment() {
count++;
}
// file2.c
extern int count;
#include <stdio.h>
void printCount() {
printf("Count: %d\n", count);
}
This code shows a variable declared in one file and accessed in another using extern.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and modifying the variable
count. - How many times: Each time
increment()orprintCount()is called.
Accessing an extern variable is a simple operation that does not depend on input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 variable accesses |
| 100 | 100 variable accesses |
| 1000 | 1000 variable accesses |
Pattern observation: The number of operations grows directly with how many times the variable is accessed, but each access is constant time.
Time Complexity: O(n)
This means the total time grows linearly with the number of times the extern variable is accessed or modified.
[X] Wrong: "Using extern makes variable access slower because it is declared outside the file."
[OK] Correct: Extern just tells the compiler the variable is defined elsewhere; accessing it is just like any other variable access and takes constant time.
Understanding how extern variables behave helps you reason about program structure and performance, a useful skill in many coding situations.
"What if the extern variable was an array and we looped over it? How would the time complexity change?"
Practice
extern storage class in C?Solution
Step 1: Understand the role of
Theexternexternkeyword tells the compiler that the variable or function is defined elsewhere, usually in another file.Step 2: Compare with other storage classes
Other options like local variables or constants do not useextern. It specifically helps share variables/functions across files.Final Answer:
To declare a variable or function defined in another file -> Option AQuick Check:
externdeclares external variables [OK]
- Confusing extern with static
- Thinking extern allocates memory
- Using extern for local variables
count?Solution
Step 1: Recall correct extern declaration syntax
The correct syntax placesexternbefore the type:extern int count;Step 2: Check invalid options
int extern count; reverses order, which is invalid. extern int count = 0; initializes the extern variable, which is not allowed. static extern int count; combines static and extern, which conflicts.Final Answer:
extern int count; -> Option BQuick Check:
extern before type, no initialization [OK]
- Initializing extern variables
- Placing extern after type
- Combining extern with static
// file1.c
int count = 5;// file2.c
extern int count;
printf("%d", count);What will be the output when these files are compiled and run together?
Solution
Step 1: Understand extern linking
The variablecountis defined infile1.cwith value 5. Theexterndeclaration infile2.crefers to the same variable.Step 2: Predict output
Sincecountis defined and initialized infile1.c, printing it infile2.coutputs 5.Final Answer:
5 -> Option AQuick Check:
extern links to defined variable value [OK]
- Assuming extern creates a new variable
- Expecting zero as default
- Ignoring multi-file linking
extern int value = 10;
int main() {
printf("%d", value);
return 0;
}
Solution
Step 1: Check extern initialization rules
Anexterndeclaration cannot initialize the variable. Initialization must be done in exactly one place without extern.Step 2: Identify error in code
The code tries to initializevaluewith 10 in the extern declaration, which causes a compilation error.Final Answer:
extern variable cannot be initialized -> Option CQuick Check:
extern declarations must not initialize [OK]
- Initializing extern variables
- Confusing extern with normal definitions
- Ignoring linker errors
// file1.c
int total = 100;
void update() { total += 50; }// file2.c
extern int total;
void update();
int main() {
update();
printf("%d", total);
return 0;
}What will be the output when compiled and run together?
Solution
Step 1: Understand extern and function usage
totalis defined infile1.cand declared extern infile2.c. The functionupdate()modifiestotal.Step 2: Trace program execution
Main callsupdate(), which adds 50 to total (100 + 50 = 150). Then it prints total, so output is 150.Final Answer:
150 -> Option DQuick Check:
Extern shares variable; function updates value [OK]
- Expecting original value without update
- Forgetting to declare extern
- Confusing declaration and definition
Ready to prove it?
Pass the test and this pattern moves unknown
