C Scope rules
In any kind of programming, scope is the area in which variables defined in a program exist; beyond that area, the variables cannot be accessed. In the C language, there are three places where variables can be declared:
- Inside a function or blockLocalVariable
- Outside all functionsglobalVariable
- InformIn the function parameter definitions
Let's take a look at whatLocalvariables,globalvariables andformparameters.
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 of using local variables. Here, all variables a, b, and c are local variables of the main() function.
Example
Global variables
Global variables are defined outside functions, usually at the top of the program. Global variables remain valid throughout the entire program lifecycle, and can be accessed inside any function.
Global variables can be accessed by any function. That is, after a global variable is declared, it is available throughout the entire program. The following is an example of using global and local variables:
Example
In a program, local variables and global variables can have the same name, but inside a function, if two names are the same, the local variable value is used, and the global variable is not used. Here is an example:
In a program, local variables and global variablesExample
When the above code is compiled and executed, it produces the following results:
value of g = 10
Formal parameters
Function parameters, i.e., formal parameters, are treated as local variables within that function, and if they have the same name as global variables, they take precedence. Here is an example:
Example
When the above code is compiled and executed, it produces the following results:
value of a in main() = 10 value of a in sum() = 10 value of b in sum() = 20 value of c in main() = 30
The difference between global variables and local variables in memory:
- Global variables are stored in the global storage area of memory, occupying static storage units;
- Local variables are stored in the stack, and only when the function in which they are located is called are storage units dynamically allocated for the variables.
For more information, see:Usage of static in C/C++: global variables 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 defining a global variable, the system automatically initializes it, as shown below:
| Data types | Initialize default values |
|---|---|
| int | 0 |
| char | '\0' |
| float | 0 |
| double | 0 |
| pointer | NULL |
Correctly initializing variables is a good programming practice; otherwise, the program may sometimes produce unexpected results, because uninitialized variables can cause garbage values already available in memory locations.
other extensions