0
0
Cprogramming~10 mins

Scope of variables - Interactive Code Practice

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