0
0
Cprogramming~10 mins

Lifetime and scope comparison - 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 variable with block scope.

C
void function() {
    [1] int x = 5;
}
Drag options to blanks, or click blank then click option'
Aauto
Bstatic
Cextern
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' inside a function makes the variable retain its value between calls, changing its lifetime.
2fill in blank
medium

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

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

Fix the error in the code to correctly declare an external variable.

C
[1] int count;

void function() {
    count = 5;
}
Drag options to blanks, or click blank then click option'
Astatic
Bauto
Cextern
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' limits the variable to the current file scope.
4fill in blank
hard

Fill both blanks to declare a global variable with internal linkage and initialize it.

C
[1] int [2] = 10;
Drag options to blanks, or click blank then click option'
Astatic
Bextern
Ccount
Dauto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extern' at global scope declares but does not define the variable.
5fill in blank
hard

Fill all three blanks to create a function that uses a static local variable and prints it.

C
void [1]() {
    [2] int [3] = 0;
    [3]++;
    printf("%d", [3]);
}
Drag options to blanks, or click blank then click option'
Aincrement
Bstatic
Ccounter
Dauto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'auto' resets the variable each call.