Bird
Raised Fist0
Cprogramming~20 mins

Static storage class - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the static keyword do when used with a variable inside a function in C?
easy
A. Keeps the variable value between function calls
B. Makes the variable global
C. Deletes the variable after the function ends
D. Allows the variable to be accessed from other files

Solution

  1. Step 1: Understand static inside a function

    A static variable inside a function keeps its value even after the function finishes.
  2. Step 2: Compare with other options

    Static does not make variables global or accessible outside the file; it preserves value only inside the function.
  3. Final Answer:

    Keeps the variable value between function calls -> Option A
  4. Quick Check:

    Static inside function = value preserved [OK]
Hint: Static inside function means value stays between calls [OK]
Common Mistakes:
  • Thinking static makes variable global
  • Assuming static deletes variable after function ends
  • Confusing static with extern keyword
2. Which of the following is the correct way to declare a static variable inside a function in C?
easy
A. static count int = 0;
B. static int count = 0;
C. int static count = 0;
D. count static int = 0;

Solution

  1. Step 1: Recall correct static syntax

    The correct syntax places static before the type: static int count = 0;.
  2. Step 2: Check other options

    Other options have wrong order of keywords, which is invalid in C.
  3. Final Answer:

    static int count = 0; -> Option B
  4. Quick Check:

    Static keyword comes before type [OK]
Hint: Static always comes before the type in declaration [OK]
Common Mistakes:
  • Placing static after the variable name
  • Mixing order of keywords
  • Using invalid syntax like 'static count int'
3. What will be the output of the following C code?
void func() {
    static int x = 0;
    x++;
    printf("%d ", x);
}

int main() {
    for(int i = 0; i < 3; i++) {
        func();
    }
    return 0;
}
medium
A. 1 1 1
B. 0 1 2
C. 1 2 3
D. 3 3 3

Solution

  1. Step 1: Understand static variable behavior in func()

    The static variable x is initialized once to 0 and keeps its value between calls.
  2. Step 2: Trace the loop calls

    Each call increments x by 1 and prints it. So outputs are 1, then 2, then 3.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    Static variable increments across calls [OK]
Hint: Static variable keeps incrementing each call [OK]
Common Mistakes:
  • Assuming static resets each call
  • Confusing initial value with printed value
  • Expecting output to be zeros
4. Identify the error in the following code snippet:
void func() {
    static int x;
}

int main() {
    x = 5;
    func();
    return 0;
}
medium
A. No error, code runs fine
B. Missing initialization of static variable x
C. Static variables cannot be declared inside functions
D. Static variable x cannot be assigned outside its function

Solution

  1. Step 1: Check variable scope

    The static variable x is declared inside func(), so it is local to that function.
  2. Step 2: Analyze assignment in main()

    Trying to assign x = 5; in main() causes an error because x is not visible outside func().
  3. Final Answer:

    Static variable x cannot be assigned outside its function -> Option D
  4. Quick Check:

    Static inside function = local scope [OK]
Hint: Static inside function is local, can't access outside [OK]
Common Mistakes:
  • Thinking static variables are global
  • Ignoring variable scope rules
  • Assuming static variables must be initialized
5. Consider two C files: file1.c and file2.c.

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?
hard
A. Compilation error due to undefined reference to count
B. Prints 1
C. Prints 0
D. Runtime error due to static variable

Solution

  1. Step 1: Understand static at file scope

    The static int count has internal linkage, visible only in file1.c.
  2. Step 2: Function access

    increment() and getCount() are external functions that can access the static count since they are in the same file.
  3. Step 3: Analyze extern usage

    The declaration extern int count; in file2.c expects a global variable count defined elsewhere, but since count is static in file1.c, it is not visible externally, causing a linker error.
  4. Final Answer:

    Compilation error due to undefined reference to count -> Option A
  5. Quick Check:

    Static variables have internal linkage, extern cannot access them [OK]
Hint: Static variables limited to file scope, extern can't access [OK]
Common Mistakes:
  • Assuming static variables are global across files
  • Expecting extern to find static variables
  • Ignoring linker errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard