Bird
Raised Fist0
Cprogramming~10 mins

Scope of variables - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a global variable.

C
#include <stdio.h>

[1] count = 10;

int main() {
    printf("Count is %d\n", count);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aextern
Bint
Cstatic
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' limits the variable to the file scope but is not necessary here.
Using 'extern' without defining the variable causes linker errors.
Using 'register' is only for local variables and hints to store in CPU registers.
2fill in blank
medium

Complete the code to declare a local variable inside main.

C
#include <stdio.h>

int main() {
    [1] number = 5;
    printf("Number is %d\n", number);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aint
Bstatic
Cextern
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extern' inside a function is incorrect for local variables.
Using 'static' inside a function changes the variable lifetime but is not required here.
Using 'register' is optional and hints to store in CPU registers.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword for the variable inside the function.

C
#include <stdio.h>

int count = 20;

int main() {
    [1] count = 10;
    printf("Count is %d\n", count);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aint
Bstatic
Cextern
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extern' inside a function without definition causes errors.
Using 'static' changes lifetime but is not necessary here.
Using 'register' is optional and unrelated to the error.
4fill in blank
hard

Fill both blanks to declare a static variable inside a function and initialize it.

C
#include <stdio.h>

void func() {
    [1] int counter = [2];
    counter++;
    printf("Counter: %d\n", counter);
}

int main() {
    func();
    func();
    return 0;
}
Drag options to blanks, or click blank then click option'
Astatic
B10
C0
Dextern
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extern' inside a function is invalid.
Initializing static variables to 10 will start counting from 10, not zero.
Not using 'static' will reset the variable each call.
5fill in blank
hard

Fill all three blanks to create a function that modifies a global variable.

C
#include <stdio.h>

[1] total = 0;

void add(int value) {
    total [2]= value;
}

int main() {
    add([3]);
    printf("Total: %d\n", total);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astatic
B+
C5
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' for global variable limits its scope to the file.
Using '-' instead of '+' will subtract instead of add.
Calling add with no argument or wrong value causes errors.

Practice

(1/5)
1. What is the scope of a variable declared inside a function in C?
easy
A. It can be accessed only in the main function.
B. It is global and can be accessed anywhere in the program.
C. It is local to that function and cannot be accessed outside.
D. It is accessible only in loops inside the function.

Solution

  1. Step 1: Understand variable declaration inside a function

    A variable declared inside a function is created when the function starts and destroyed when it ends.
  2. Step 2: Identify the scope of such variables

    These variables are local to the function and cannot be accessed outside it.
  3. Final Answer:

    It is local to that function and cannot be accessed outside. -> Option C
  4. Quick Check:

    Local variable scope = inside function only [OK]
Hint: Local variables stay inside their function only [OK]
Common Mistakes:
  • Thinking local variables are global
  • Assuming local variables exist outside function
  • Confusing local with static variables
2. Which of the following is the correct way to declare a global variable in C?
easy
A. int x; // declared outside any function
B. int x; inside main()
C. static int x; inside a function
D. int x; inside a loop

Solution

  1. Step 1: Identify global variable declaration

    Global variables are declared outside all functions, usually at the top of the file.
  2. Step 2: Check each option

    int x; // declared outside any function declares int x; outside any function, which is correct for global scope.
  3. Final Answer:

    int x; // declared outside any function -> Option A
  4. Quick Check:

    Global variables declared outside functions [OK]
Hint: Globals declared outside all functions [OK]
Common Mistakes:
  • Declaring globals inside functions
  • Using static inside functions for globals
  • Declaring variables inside loops thinking they are global
3. What will be the output of the following C code?
int x = 5;
void func() {
    int x = 10;
    printf("%d", x);
}
int main() {
    func();
    printf(" %d", x);
    return 0;
}
medium
A. 10 10
B. 5 10
C. 5 5
D. 10 5

Solution

  1. Step 1: Analyze variable scopes in func()

    Inside func(), a local variable x is declared with value 10, which hides the global x.
  2. Step 2: Check printf outputs

    First printf prints local x (10). Then main prints global x (5).
  3. Final Answer:

    10 5 -> Option D
  4. Quick Check:

    Local hides global inside function [OK]
Hint: Local variable hides global inside function [OK]
Common Mistakes:
  • Assuming global x prints inside func()
  • Mixing order of printed values
  • Ignoring local variable shadowing
4. Identify the error in the following C code snippet:
void func() {
    printf("%d", x);
}
int main() {
    int x = 5;
    func();
    return 0;
}
medium
A. x is not declared globally, so func() cannot access it
B. Variable x is used before declaration in func()
C. Missing return type for main()
D. Syntax error in printf statement

Solution

  1. Step 1: Check variable x usage in func()

    func() tries to print x but x is declared only inside main(), so func() cannot see it.
  2. Step 2: Understand variable scope rules

    Variables declared inside main() are local to main() only, not visible in other functions.
  3. Final Answer:

    x is not declared globally, so func() cannot access it -> Option A
  4. Quick Check:

    Local variables not visible outside their function [OK]
Hint: Functions can't access locals from other functions [OK]
Common Mistakes:
  • Thinking main()'s x is global
  • Ignoring variable scope rules
  • Assuming implicit global variables
5. Consider the following C code:
int x = 1;
void outer() {
    int x = 2;
    {
        int x = 3;
        printf("%d ", x);
    }
    printf("%d ", x);
}
int main() {
    outer();
    printf("%d", x);
    return 0;
}

What is the output when this program runs?
hard
A. 1 2 3
B. 3 2 1
C. 3 3 3
D. 2 3 1

Solution

  1. Step 1: Analyze innermost block variable

    The innermost block declares x = 3, so first printf prints 3.
  2. Step 2: Analyze outer block variable

    After inner block ends, the x = 3 goes out of scope, so next printf prints outer x = 2.
  3. Step 3: Analyze global variable in main()

    Finally, main prints global x = 1.
  4. Final Answer:

    3 2 1 -> Option B
  5. Quick Check:

    Inner blocks hide outer variables [OK]
Hint: Inner block variables override outer ones [OK]
Common Mistakes:
  • Ignoring block-level scope
  • Assuming all x print same value
  • Confusing order of prints
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