The static storage class in C helps keep a variable or function's value or state between uses, and controls its visibility in the program.
Static storage class
static data_type variable_name = initial_value;The static keyword can be used with variables inside functions or outside functions.
Static variables inside functions keep their value between calls but are only visible inside that function.
void count_calls() { static int count = 0; count++; printf("Called %d times\n", count); }
static int file_var = 10; void print_var() { printf("Value: %d\n", file_var); }
This program shows how a static variable inside a function keeps its value between calls.
#include <stdio.h> void demo_static() { static int counter = 0; counter++; printf("Counter is %d\n", counter); } int main() { demo_static(); demo_static(); demo_static(); return 0; }
Static variables are initialized only once, when the program starts or when the function is first called.
Static variables inside functions are not destroyed when the function ends.
Using static for global variables or functions limits their scope to the file, helping avoid name conflicts.
Static keeps variables alive between function calls.
Static limits variable or function visibility to the file or function.
Use static to remember values or hide details inside a file.
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
