C Scope Rules
# C Scope Rules
In any programming language, a scope is the region of the program where a defined variable can be accessed. Beyond that region, the variable cannot be accessed. In C, there are three places where variables can be declared:
1. **Local** variables β inside a function or block
2. **Global** variables β outside of all functions
3. **Formal** parameters β in the function parameter definitions
Let's look at what **local** variables, **global** variables, and **formal** parameters are.
## Local Variables
Variables declared inside a function or block are called local variables. They can only be used by statements inside that function or code block. Local variables are not known outside the function. Here is an example using local variables. Here, all variables a, b, and c are local to the main() function.
## Example
#includeint main(){/* Local Variable Declaration */int a, b; int c; /* Actual Initialization */a = 10; b = 20; c = a + b; printf("value of a = %d, b = %d and c = %dn", a, b, c); return 0; }
## Global Variables
Global variables are defined outside of a function, usually at the top of the program. Global variables hold their value throughout the lifetime of the program and can be accessed inside any of the functions in the program.
Global variables can be accessed by any function. That is, a global variable is available throughout the entire program after its declaration. Here is an example using both global and local variables:
## Example
#include/* Global Variable Declaration */int g; int main(){/* Local Variable Declaration */int a, b; /* Actual Initialization */a = 10; b = 20; g = a + b; printf("value of a = %d, b = %d and g = %dn", a, b, g); return 0; }
A program can have the same name for both local and global variables, but inside the function, the local variable will be used if the two names are the same. The global variable will not be used. Here is an example:
A program can have the same name for both local and global variables.
## Example
#include/* Global Variable Declaration */int g = 20; int main(){/* Local Variable Declaration */int g = 10; printf("value of g = %dn", g); return 0; }
When the above code is compiled and executed, it produces the following result:
value of g = 10
## Formal Parameters
Function parameters, also known as formal parameters, are treated as local variables within that function and take precedence over global variables if they share the same name. Here is an example:
## Example
#include/* Global Variable Declaration */int a = 20; int main(){/* Local Variable Declaration in the Main Function */int a = 10; int b = 20; int c = 0; int sum(int, int); printf("value of a in main() = %dn", a); c = sum(a, b); printf("value of c in main() = %dn", c); return 0; }/* Function to Add Two Integers */int sum(int a, int b){printf("value of a in sum() = %dn", a); printf("value of b in sum() = %dn", b); return a + b; }
When the above code is compiled and executed, it produces the following result:
value of a in main() = 10 value of a in sum() = 10 value of b in sum() = 20 value of c in main() = 30
> **Difference between global and local variables in memory**:
>
>
> * Global variables are stored in the global storage area of memory, occupying static storage units;
> * Local variables are stored on the stack, and storage units are dynamically allocated for the variables only when the function they belong to is called.
>
>
> For more information, see: [Usage of static in C/C++ β Global and Local Variables](#)
## Initializing Local and Global Variables
When a local variable is defined, the system does not initialize it β you must initialize it yourself. When a global variable is defined, the system automatically initializes it with the following default values:
| Data Type | Default Initialization Value |
| --- | --- |
| int | 0 |
| char | '' |
| float | 0 |
| double | 0 |
| pointer | NULL |
Properly initializing variables is a good programming practice. Otherwise, the program may sometimes produce unexpected results, because uninitialized variables can result in garbage values that are already present at those memory locations.
YouTip