Language variables for type C and storage methods

Source: Internet
Author: User

Scope of variable: valid range variable"Local Variables

1. In the function of the local variable, the amount of the function after the end of its own initiative to revoke

2. A local variable in a compound statement within a function (for example, for a statement), this variable is voluntarily revoked after the compound statement ends. This variable can have the same name as the variable in 1, the variable permissions defined in the compound statement scope are higher, considering the readability is not recommended

Like what:

#include <stdio.h> #include <stdlib.h>void fun1 (); int main () {fun1 ();} void Fun1 () {int m=200;int i=0;for (i=0;i<1;i++) {int m=5;printf ("%d", m);} printf ("%d\n", m); System ("Pause");} Output Result: 5  

"Global variables

Variables that are defined outside of all functions need to be noted and differentiated by the definition and declaration of variables. Only variables defined outside of the entire function are global variables, so this variable is not necessarily the first few lines of code in a file, assuming that the variable is defined behind a function. And this function needs to use this variable, then this variable must be declared before the function or within the function of the declaration can be used, assuming that the variable is in front of this function, then can omit the declaration.

Defines the general form of a variable: the extern type specifier variable name (for example: int A, when defined, extern can be omitted.) )

Declares the general form of a variable: extern type specifier variable name (extern cannot be omitted when declaring a variable)

How variables are stored: How they are stored determines the lifetime of the function"Static storageBe able to define a ' keyword static (descriptive description of the variable) extern (descriptive narrative external variable)Static Storageof thevariables', note the wording between the arguments, which means that static variables are used for static storage, while global variables are also used for static storage, static variables and static storage are not a means
· Static variables persist, allocating memory space in the pre-programmed phase until the end of the program is destroyed"Dynamic storageAble to use keyword auto (descriptive narrative of its own active variable, assuming that a variable does not write descriptive descriptors then is the default auto) register (descriptive narrative register variable) to define aDynamic Storageof thevariables。
The dynamic variable is allocated at the beginning of the function or the compound statement, and at the end it voluntarily clears the memory
For example, the function's formal participation, does not allocate the memory when the function is defined, only has the allocation at the call. Voluntarily frees memory after the call ends
Compound statement is also a reason
Global and local of static variables"Static local variablesStatic local variables are defined within a function. His life cycle is the entire program, but its scope is still the same as its own active variable, can only be used in the function that defines the variable, although the variable continues to exist after the function call, but can not directly access to it, it is assumed that the entire program during the operation of the function that defines it again, Within this function, you cancontinue toWith this variable, the value of the variable is the state at the end of the last function call. This is very practical. Of course, all variables can achieve the same effect, but easy has side effects."Static global variablesThis has been in effect during the run of the program,

It is important to note that static global variables and non-static global variables (global variables)

A program often has very much source code. One source file contains another source file,

In summary, a global variable is valid in all of its source files, whereas a static global variable is only valid in the source file in which it is defined,

The general conclusion: changing the storage of a local variable rather than a static variable changes its life cycle, and changes its scope by a global variable static variable. Limit its scope

Language variables for type C and storage methods

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: 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.