Global variable Management in C language and global variable Management in C Language
The information encapsulation in C language is weak, and only the File Scope of static variables is available.
Without constraints, it is easy to cause global variables to fly.
If you define a global struct and put all the global variables in this GlobleVariate, you should take care of it, at least a little more elegant than streaking.
Furthermore, each module defines its own struct and places its public variables in the struct. In this way, it is more elegant to split GlobleVariate into multiple small structs.
The possible pseudocode is as follows.
Struct GlobleVariate
{
Struct ModuleVariate1;
Struct ModuleVariate2;
...
}
At the same time, defining variables outside the function is prohibited, which can improve global variable management.
Advantages and disadvantages of defining global variables in C Language Programs
The advantage is that it is easy to pass parameters and data can be shared throughout the program. You do not need to transmit parameters in a very troublesome way, but also save the time for passing parameters, which reduces the running time of the program.
The disadvantage is that global variables are not well controlled, which is not conducive to program structuring. Because all functions in the program can modify global variables at will, they may cause unpredictable errors and are not easy to debug. In addition, global variables may conflict with local variables, causing program confusion.
Therefore, we recommend that you try not to use or use less local variables.
How to define and reference global variables in C language?
For example
# Include <stdio. h>
Int a = 0; // isn't a global variable a defined here?
Int main ()
{
Printf ("% d", );
}