Bird
Raised Fist0
Cprogramming~20 mins

Lifetime and scope comparison - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Lifetime and Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of variable with block scope
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int x = 5;
    {
        int x = 10;
        printf("%d ", x);
    }
    printf("%d\n", x);
    return 0;
}
A10 10
B5 10
C10 5
D5 5
Attempts:
2 left
💡 Hint
Remember that variables declared inside a block have their own scope.
❓ 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() {
    counter();
    counter();
    counter();
    return 0;
}
A1 2 3
B0 1 2
C1 1 1
D3 3 3
Attempts:
2 left
💡 Hint
Static variables keep their value between function calls.
🔧 Debug
advanced
2:00remaining
Identify the error with variable lifetime
What error will this C code produce when compiled or run?
C
#include <stdio.h>

int* getPointer() {
    int x = 10;
    return &x;
}

int main() {
    int* p = getPointer();
    printf("%d\n", *p);
    return 0;
}
ARuntime error: segmentation fault or undefined behavior
BCompilation error: returning address of local variable
COutput: 10
DCompilation warning but runs and prints 0
Attempts:
2 left
💡 Hint
Think about the lifetime of local variables after the function returns.
🧠 Conceptual
advanced
2:00remaining
Which variable has static lifetime?
Given these variable declarations, which one has static lifetime and global scope?
C
int global_var;

void func() {
    static int static_var = 0;
    int local_var = 0;
}
AAll have static lifetime
Bglobal_var
Clocal_var
Dstatic_var
Attempts:
2 left
💡 Hint
Global variables exist for the whole program run.
❓ Predict Output
expert
2:00remaining
Output of nested static and automatic variables
What is the output of this C program?
C
#include <stdio.h>

void f() {
    static int s = 0;
    int a = 0;
    s++;
    a++;
    printf("%d %d\n", s, a);
}

int main() {
    f();
    f();
    f();
    return 0;
}
A
0 1
1 1
2 1
B
1 1
1 1
1 1
C
1 0
2 0
3 0
D
1 1
2 1
3 1
Attempts:
2 left
💡 Hint
Static variables keep their value between calls; automatic variables reset each call.

Practice

(1/5)
1. Which of the following best describes the scope of a variable in C?
easy
A. The time duration the variable exists in memory
B. The amount of memory the variable uses
C. The part of the program where the variable can be accessed
D. The data type of the variable

Solution

  1. Step 1: Understand the meaning of scope

    Scope means where in the code a variable can be used or accessed.
  2. Step 2: Differentiate scope from lifetime

    Lifetime is about how long the variable exists in memory, not where it can be used.
  3. Final Answer:

    The part of the program where the variable can be accessed -> Option C
  4. Quick Check:

    Scope = where variable is accessible [OK]
Hint: Scope means where variable is visible in code [OK]
Common Mistakes:
  • Confusing scope with lifetime
  • Thinking scope is about memory size
  • Mixing scope with data type
2. Which declaration correctly creates a variable with local scope and static lifetime inside a function?
easy
A. static int count = 0;
B. int static count = 0;
C. auto static int count = 0;
D. register static int count = 0;

Solution

  1. Step 1: Recall correct syntax for static local variable

    The keyword static comes before the type: static int count = 0;.
  2. Step 2: Identify invalid syntax options

    int static count = 0; has incorrect order. auto static int count = 0; and register static int count = 0; use invalid combinations of storage class specifiers.
  3. Final Answer:

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

    Static local variable syntax = static before type [OK]
Hint: Put 'static' before type for static local variable [OK]
Common Mistakes:
  • Placing 'static' after type
  • Combining 'auto' and 'static' incorrectly
  • Using 'register' with 'static'
3. What is the output of this 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 2 3
B. 1 1 1
C. 0 1 2
D. 3 3 3

Solution

  1. Step 1: Understand static variable behavior inside function

    Static variable x keeps its value between function calls and starts at 0.
  2. Step 2: Trace the loop and function calls

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

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

    Static variable increments across calls = 1 2 3 [OK]
Hint: Static variables keep value between calls [OK]
Common Mistakes:
  • Assuming static resets each call
  • Confusing initial value of static variable
  • Ignoring the loop effect
4. Identify the error in this code snippet related to variable scope:
int main() {
    if (1) {
        int a = 5;
    }
    printf("%d", a);
    return 0;
}
medium
A. Variable 'a' should be declared static
B. Missing semicolon after variable declaration
C. printf format specifier is incorrect
D. Variable 'a' is used outside its scope

Solution

  1. Step 1: Check where 'a' is declared and used

    'a' is declared inside the if-block and cannot be accessed outside it.
  2. Step 2: Understand scope rules

    Variables declared inside braces have local scope limited to those braces, so 'a' is out of scope in printf.
  3. Final Answer:

    Variable 'a' is used outside its scope -> Option D
  4. Quick Check:

    Using variable outside its block scope causes error [OK]
Hint: Variables only visible inside their braces [OK]
Common Mistakes:
  • Trying to use variable outside its block
  • Thinking static fixes scope errors
  • Ignoring variable lifetime vs scope
5. Consider this code:
void counter() {
    static int count = 0;
    int temp = 0;
    count++;
    temp++;
    printf("%d %d\n", count, temp);
}

int main() {
    for (int i = 0; i < 3; i++) {
        counter();
    }
    return 0;
}
What will be the output and why?
hard
A. 1 1 2 2 3 3 Because both variables keep their values between calls
B. 1 1 2 1 3 1 Because 'count' is static and 'temp' is local each call
C. 1 0 1 0 1 0 Because 'temp' is never incremented properly
D. 0 0 0 0 0 0 Because variables are not initialized

Solution

  1. Step 1: Analyze 'count' variable behavior

    'count' is static, so it keeps incrementing across calls: 1, 2, 3.
  2. Step 2: Analyze 'temp' variable behavior

    'temp' is local and reinitialized to 0 each call, so it increments to 1 every time.
  3. Step 3: Understand output pattern

    Output lines are: "1 1", "2 1", "3 1" matching the behavior above.
  4. Final Answer:

    1 1 2 1 3 1 Because 'count' is static and 'temp' is local each call -> Option B
  5. Quick Check:

    Static keeps value; local resets each call [OK]
Hint: Static persists, local resets each function call [OK]
Common Mistakes:
  • Assuming local variable keeps value between calls
  • Confusing initialization of static vs local
  • Ignoring that temp resets every call
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