📖 Scenario: Imagine you are organizing a small event and need to keep track of the number of guests in different rooms. You want to understand how variables behave inside and outside functions in C.
🎯 Goal: You will create a simple C program to compare the lifetime and scope of variables declared globally, locally inside a function, and as static inside a function.
📋 What You'll Learn
Create a global variable called guest_count initialized to 10
Create a function called update_guest_count that declares a local variable guest_count initialized to 5
Inside update_guest_count, declare a static variable static_guest_count initialized to 0 and increment it by 1 each time the function is called
Print the values of the global guest_count, local guest_count, and static_guest_count inside the function
Call update_guest_count three times from main and print the global guest_count after each call
💡 Why This Matters
🌍 Real World
Understanding variable lifetime and scope helps in managing data correctly in programs, avoiding bugs related to unexpected variable changes.
💼 Career
Many programming jobs require knowledge of how variables behave in memory and across function calls, especially in systems programming and embedded development.
Progress0 / 4 steps
1
Create the global variable
Create a global integer variable called guest_count and set it to 10.
C
Hint
Global variables are declared outside any function.
2
Create the update_guest_count function
Create a function called update_guest_count that declares a local integer variable guest_count set to 5. Inside the function, declare a static integer variable static_guest_count initialized to 0. Increment static_guest_count by 1 each time the function is called.
C
Hint
Static variables keep their value between function calls.
3
Print variable values inside update_guest_count
Inside the update_guest_count function, add printf statements to print the values of the global guest_count (print this before declaring the local variable), the local guest_count, and the static_guest_count.
C
Hint
Use printf to show values. To access and print the global guest_count, place its printf before declaring the local guest_count to avoid shadowing.
4
Call update_guest_count and print global guest_count in main
Create a main function that calls update_guest_count three times. After each call, print the global guest_count using printf.
C
Hint
Remember to include return 0; in main.
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.