Lifetime and Scope Comparison in C
📖 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 10Create a function called
update_guest_count that declares a local variable guest_count initialized to 5Inside
update_guest_count, declare a static variable static_guest_count initialized to 0 and increment it by 1 each time the function is calledPrint the values of the global
guest_count, local guest_count, and static_guest_count inside the functionCall
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