Variable Scope - Local and Global Variables
Local variables
Local variables in C are only visible and accessible within the function/block where they are declared, and cannot be accessed or modified from outside the function/block.
Local variables are often used to store temporary values, intermediate results, or to pass values from one part of the function to another.
They are created when the function is called or when the block starts and are destroyed when the function returns or when the block ends.
Example - Local variables
Here's an example of using local variables within a function:
#include <stdio.h>
int add(int x, int y)
{
int sum; // Declare a local integer variable named sum
sum = x + y; // Assign the result of the addition to sum
return sum;
}
int main()
{
int result = add(3, 5); // Call the add function
printf("The result of adding 3 and 5 is %d\n", result); // Print the result
return 0;
}
Output:
Explanation
The
add
function declares a local variablesum
, which is used to store the result of the addition.This variable is created when the function is called, and is destroyed when the function returns.
The result of the addition is then returned from the function and assigned to the variable
result
in the calling code.
Global variables
Global variables in C are created when the program starts and remain in memory until the program ends.
They are declared outside of any function/block and are accessible from any function within the same file.
They are not destroyed when a function returns, and their values can be accessed and modified by any function in the program.
Global variables are often used to store values that are shared by multiple functions, such as constant values, or values that persist across multiple function calls.
Example - Global variables
Here's an example of using a global variable in C:
#include <stdio.h>
int global_value = 0; // Declare a global variable
void increment()
{
global_value++; // Increment the global variable
}
int main()
{
increment(); // Call the increment function
printf("The value of the global variable is %d\n",
global_value); // Print the value of the global variable
return 0;
}
Output:
Explanation
The variable
global_value
is declared outside of any function and is therefore accessible from any function in the same file.The
increment
function increments the value of this variable, and themain
function prints its value.
Global variables can be accessed and modified by any function in the same file, and this can lead to unintended side effects and difficult-to-debug code.
As a best practice, it is usually recommended to limit the use of global variables and to use them only when they are necessary.
Difference - Local and Global variables
Here's a table summarizing the differences between local and global variables in C:
Feature | Local Variables | Global Variables |
---|---|---|
Declaration | Declared within a function | Declared outside of any function |
Accessibility | Accessible only within the function where they are declared | Accessible from any function within the same file |
Lifetime | Created when the function is called and destroyed when the function returns | Created when the program starts and remain in memory until the program ends |
Initialization | Must be initialized before use | Can be initialized at declaration or later in the program |
Scope | Limited to the function where they are declared | Available throughout the entire program |