The difference between a c-language local variable and a global variable. --arvin

Source: Internet
Author: User

a local variable is a value stored in the entire class using the same memory Block. The existence of global variables is mainly due to the following reasons: 1, The use of global variables will occupy more memory (because of their lifetime), but in the computer configuration is very high today, this is not a problem, unless the use of large objects of global variables, can be avoided must be avoided. 2, using the global variables program runs faster (because memory does not need to be redistributed), and now it is not much faster. 3, for the namespace pollution of local variables, this can be avoided by not using too many Variables. in summary, Global variables can be used, but global variables should be used with care to make the name of the period as easy to understand, and not too short, to avoid namespace pollution, and avoid the use of large object global Variables. description of the local variable:In a program, A variable that can be accessed only in a particular procedure or function is relative to a global variable. a Global variable, also known as an external variable, is defined outside the function and is scoped to the end of the program file, starting at the definition of the Variable. The global variables are all stored in the static storage area, and when the program starts execution, the global variable is allocated the store, and the program line is Released. Local variables can have the same name as global variables, but local variables mask global variables. When a variable is referenced within a function, local variables of the same name are used instead of global variables. Differentiated according to the scope of Use. 1. Local VariablesHe means a variable scoped to define a local variable within a function that means he can only use it in defining his function .the most notable is that only when the program executes to define his module can it be generated, and once execution exits the module the variable disappearsEg.
func () {   int  x;   The scope of the local variable x is clear ...   }
2. Global Variableseffective in the process of program execution
int x=1; func () {x=x+1;} Func1 () {  x=x-1;} Main () {}
it is not difficult to see the scope of the integral type Xfor global variables, The system will automatically assign a value of 0 if it is not initialized at the time of Definition.the character type is empty ' + 'the drawbacks of global variables increase the commonality of memory overhead decrease functionwhen defining global variables, the ideal location is at the beginning of the file, and when these functions and some functions in other source program files in the same program need to use the global variable, use extern to describe the variable inside the function, stating that he is external. (detailed explanations are also needed here.)
main () {externint  a,b;printf ("min is%d\n", Min ( a, b));} int a=1, b=2; int min (x, y) int x, y; {int  z;z=x<y? x:y; return (z);}
I would also like to state that the description of the external variable and the definition of the global variable are not the same thing. the description of an external variable simply declares that the variable is externally defined as a global variable referenced here and the definition of a global variable is to allocate a storage unit to it. A global variable can be defined only once but is referenced more than once. In the same source file, when a global variable has the same name as a local variable, the global variable does not work within the scope of the local Variable. -----------------------------------------------------------updated: 20161009association: Http://blog.csdn.net/t5studioEmail:[email protected]

The difference between a c-language local variable and a global variable. --arvin

Related Article

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.