Global variable Management in C language and global variable Management in C Language

Source: Internet
Author: User

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", );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.