What if your variables could quietly remember their past without causing chaos everywhere?
Why Static storage class? - Purpose & Use Cases
Imagine you have a program with many functions, and you want some variables to keep their values between calls but only be used inside one function or file.
Without a special way to store them, you might try to use global variables everywhere or pass values around manually.
Using global variables everywhere can cause confusion and bugs because any part of the program can change them unexpectedly.
Passing variables around manually is tiring and error-prone, especially as the program grows.
The static storage class lets you keep variables alive between function calls but hidden from other parts of the program.
This means your variables remember their values without risking accidental changes from outside.
int count = 0; // global variable void func() { count++; printf("%d", count); }
#include <stdio.h> void func() { static int count = 0; count++; printf("%d", count); }
It enables safer, cleaner code by preserving variable values privately inside functions or files without exposing them globally.
Think of a game where you want to count how many times a player has jumped without letting other parts of the game accidentally reset or change that count.
Static variables keep their value between function calls.
They are only visible inside the function or file where declared.
This helps avoid bugs from unwanted changes and keeps code organized.
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
