0
0
Cprogramming~10 mins

Why storage classes are needed - Test Your Understanding

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

Complete the code to declare a variable with automatic storage class.

C
void function() {
    [1] int x = 10;
}
Drag options to blanks, or click blank then click option'
Astatic
Bauto
Cextern
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using static instead of auto inside functions.
2fill in blank
medium

Complete the code to declare a variable with static storage class inside a function.

C
void counter() {
    [1] int count = 0;
    count++;
    printf("%d", count);
}
Drag options to blanks, or click blank then click option'
Aauto
Bregister
Cextern
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using auto which resets the variable each call.
3fill in blank
hard

Fix the error in the code by choosing the correct storage class for a global variable.

C
[1] int global_var = 5;

void func() {
    printf("%d", global_var);
}
Drag options to blanks, or click blank then click option'
Aextern
Bauto
Cstatic
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using extern which declares a variable defined elsewhere.
4fill in blank
hard

Fill both blanks to declare an external variable and use it in another file.

C
// file1.c
[1] int shared_var = 100;

// file2.c
[2] int shared_var;

void print_var() {
    printf("%d", shared_var);
}
Drag options to blanks, or click blank then click option'
Aextern
Bstatic
Cauto
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using static in both files which hides the variable.
5fill in blank
hard

Fill all three blanks to declare a register variable, use it in a loop, and print its value.

C
void loop() {
    [1] int i;
    for (i = 0; i [2] 5; i++) {
        printf("%d ", i);
    }
    printf("\nValue of i: %d", [3]);
}
Drag options to blanks, or click blank then click option'
Aregister
B<
Ci
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using static inside the loop which changes variable lifetime.