Scope is the part of the program where a variable or function is accessible or visible.
Click to reveal answer
beginner
What does lifetime of a variable mean in C?
Lifetime is the period during program execution when the variable exists in memory and holds a value.
Click to reveal answer
intermediate
Compare local and global variable scope.
Local variables are accessible only inside the block or function they are declared in. Global variables are accessible throughout the entire program after their declaration.
Click to reveal answer
intermediate
How does the lifetime of a local variable differ from a global variable?
Local variables exist only while the function or block runs. Global variables exist for the entire program run.
Click to reveal answer
advanced
What is the lifetime and scope of a static variable inside a function?
A static variable inside a function has a lifetime for the entire program run but its scope is limited to that function only.
Click to reveal answer
Where is a local variable accessible in C?
AThroughout the entire program
BOnly inside the block or function it is declared in
COnly inside the header files
DNowhere, it is not accessible
✗ Incorrect
Local variables are only accessible inside the block or function where they are declared.
What happens to a local variable's value after the function ends?
AIt is copied to all other functions
BIt remains stored in memory
CIt becomes a global variable
DIt is lost because the variable's lifetime ends
✗ Incorrect
Local variables exist only while the function runs; after it ends, their values are lost.
What is true about a global variable's lifetime?
AIt lasts only inside one function
BIt lasts only during compilation
CIt lasts for the entire program execution
DIt lasts only inside a block
✗ Incorrect
Global variables exist for the whole time the program runs.
What does the static keyword do to a variable inside a function?
AKeeps the variable alive for the whole program but limits its scope to the function
BMakes the variable global
CDeletes the variable after the function ends
DMakes the variable accessible everywhere
✗ Incorrect
Static variables inside functions keep their value between calls but are only accessible inside that function.
Which variable has the widest scope in C?
AGlobal variable
BLocal variable
CStatic variable inside a function
DFunction parameter
✗ Incorrect
Global variables can be accessed anywhere after their declaration.
Explain the difference between scope and lifetime of a variable in C.
Think about where a variable can be used versus how long it exists.
You got /3 concepts.
Describe how a static variable inside a function behaves in terms of scope and lifetime.
Consider how static changes the usual local variable behavior.
You got /3 concepts.
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
Step 1: Understand the meaning of scope
Scope means where in the code a variable can be used or accessed.
Step 2: Differentiate scope from lifetime
Lifetime is about how long the variable exists in memory, not where it can be used.
Final Answer:
The part of the program where the variable can be accessed -> Option C
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
Step 1: Recall correct syntax for static local variable
The keyword static comes before the type: static int count = 0;.
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.
Final Answer:
static int count = 0; -> Option A
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
Step 1: Understand static variable behavior inside function
Static variable x keeps its value between function calls and starts at 0.
Step 2: Trace the loop and function calls
Each call increments x by 1 and prints it: first 1, then 2, then 3.
Final Answer:
1 2 3 -> Option A
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
Step 1: Check where 'a' is declared and used
'a' is declared inside the if-block and cannot be accessed outside it.
Step 2: Understand scope rules
Variables declared inside braces have local scope limited to those braces, so 'a' is out of scope in printf.
Final Answer:
Variable 'a' is used outside its scope -> Option D
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
Step 1: Analyze 'count' variable behavior
'count' is static, so it keeps incrementing across calls: 1, 2, 3.
Step 2: Analyze 'temp' variable behavior
'temp' is local and reinitialized to 0 each call, so it increments to 1 every time.