What is the Local Variable in C?
In C, a local variable is a variable defined inside a function or a code block. The scope and lifetime of these variables remain limited to the block in which they are declared. `Scope specifies where we can access or use the variable, and lifetime specifies how long the variable will last in memory.
Where are the Local Variables Stored in C?
When a C program is loaded, memory is allocated to it by a fixed layout that is divided primarily into four parts, which are as follows:
Code: The program's code is stored in read-only mode.
Data: The initialized and uninitialized global data is stored here.
Stack: This is used to store the activation record(information related to the execution) of the function.
Heap: All data allocated dynamically gets stored in this memory space.
Visit this link to learn more about this memory layout in C.
Every time a function is invoked, an activation record corresponding to it is created and pushed to the top of the execution stack. The activation records contain information such as the return address where the return value should be provided, the actual parameter space where the parameters are stored, a control link pointing to the activation record of the caller function, and other information. The paragraph's relevance comes from the activation record, which also stores local variables.
The inference of this entire discussion is that the local data is stored inside the activation record of the function which is further stored in the call stack of the program. This is the reason when a function goes out of scope, all its local variable also goes out of scope as the activation record of that function is been popped out of the stack.
How Local Variable Work in C?
The scope and lifetime characteristics of the variables in C can be used to categorize them. So based on this we have two types of variables in C,
- Local Variable
- Global Variable
The local variable's scope is limited until the function's activation record is at the top of the stack, which means the function's execution is ongoing. The local variables get their memory before execution. When the function completes the execution, it provides the caller function's return value and other related details of the activation record. After that, all memory allocated for local variables is released. We should always declare the local variable before using it. We could not declare more than one local variable with the same name in the same scope.
Advantages of Using Local Variables
Memory Optimization
Using a Local variable improves memory optimization, Consider a situation where you only need a variable to be in memory for a short moment during the execution of a large application. If you write the variable globally, it won't leave memory before the program ends, even though the variable's work has been completed. So we can create a block or function and declare our variable inside that so that memory associated with the variable could be released after the execution of the block.
Avoid Name Collision
In big programs, it is essential to create code that doesn't interfere with each other, let's imagine that one programmer has written a variable, and if another programmer writes the same variable, the program will raise an error. Local variables can therefore prevent this collision because they are allocated memory in the call stack and are unable to unexpectedly collide with variables declared in other functions. Therefore, we can use the same variable name across different blocks.
Provides More Security
When a variable in one section of code shouldn't be visible to another because doing so could compromise security. Local variables can therefore be helpful in this situation; we can wrap the variables in a code block or function to limit access of those variables to that scope only.
Examples of Local Variable in C
1. Local Variables Doesn't Clashes With Each Other
This example shows that name of variables doesn't create a naming collision if they belong to a different block.
Output:
As we can see in the above example both the variable of func() and main() have the same name a but we could separately access both of them within their scope.
2. Local Variables are Only Accessible by the Same Function
This example shows how the local variables are only accessible inside the block in which they have been declared.
Error Output:
In the above code, variable a is local to main(), so if we try to access it outside it error will occur.
3. Block Scoped Local Variables
We have discussed examples related to function-scoped local variables, we can also create local variables by defining blocks { //Code } in the C program.
The variables written inside blocks are independent of the outside environment and only accessible inside the block.
Error Output:
If there is the same variable naming outside and inside the block, then the block variable will shadow the other variable.
Output:
The function or block always prefers the local variable. In the above code, var1 present in the block is printed instead of var1 present outside the block.
Learn More about C Programming Tutorial
You can explore more articles related to C programming Tutorials from this link.
Conclusion
- Variables in C are categorized into two types based on scope and lifetime: local and global.
- Local variables are declared within a function or block, limiting their scope and lifetime to that specific context, and are stored in the stack memory.
- These variables enhance security by preventing access from outside functions, optimize memory usage, and reduce naming collisions.
- Despite their benefits, local variables have drawbacks, such as the overhead of passing data between modules and their limited scope, which may be restrictive in scenarios where persistent data is required.