0
0
Cprogramming~20 mins

Static storage class - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of static variable in function
What is the output of this C program?
C
#include <stdio.h>

void counter() {
    static int count = 0;
    count++;
    printf("%d ", count);
}

int main() {
    for(int i = 0; i < 3; i++) {
        counter();
    }
    return 0;
}
A1 2 3
B0 1 2
C1 1 1
D3 3 3
Attempts:
2 left
💡 Hint
Remember that static variables inside functions keep their value between calls.
Predict Output
intermediate
2:00remaining
Static variable scope in multiple files
Given two files, what will be the output when compiled and run together? File1.c: #include static int x = 5; void printX() { printf("%d\n", x); } File2.c: extern void printX(); int main() { printX(); return 0; }
A5
B0
CCompilation error
DUndefined reference error
Attempts:
2 left
💡 Hint
Static variables have internal linkage, but functions can access them within the same file.
🔧 Debug
advanced
2:00remaining
Why does this static variable reset unexpectedly?
Consider this code snippet: #include void func() { static int val = 10; printf("%d ", val); val = 5; } int main() { func(); func(); return 0; } What is the output and why?
ACompilation error
B10 10
C5 5
D10 5
Attempts:
2 left
💡 Hint
Static variables keep their value between calls, but you are changing it inside the function.
📝 Syntax
advanced
2:00remaining
Identify the syntax error with static variable usage
Which option contains a syntax error related to static variable declaration?
C
void example() {
    static int a = 0;
    a++;
}
Astatic int a = 0; inside a function is valid
Bstatic int a = 0; outside any function is valid
Cstatic int a = 0 inside a function without semicolon is invalid
Dstatic int a; outside any function is invalid
Attempts:
2 left
💡 Hint
Look for missing semicolons in the declarations described in the options.
🚀 Application
expert
2:00remaining
How many times is the static variable initialized?
Analyze this code: #include void test() { static int x = 0; printf("%d ", x); x++; } int main() { for(int i = 0; i < 4; i++) { test(); } return 0; } How many times is the static variable 'x' initialized during the program execution?
AEvery time test() is called
BOnce, before the first call to test()
COnce, at the start of main()
DNever, it is uninitialized
Attempts:
2 left
💡 Hint
Static variables inside functions are initialized only once, not every call.