Summary of C language global variables and local variables

Source: Internet
Author: User


—————————————————————————————————————————————————————————————————————————————


Only continuous learning, continuous progress, can not be replaced.


Only your irreplaceable will determine your value.


-----November 18, 2016


—————————————————————————————————————————————————————————————————————————————


The first part of the question:

today a small partner asked me a question, as Figure Program 1:



I see no problem ah, then think that there is a problem, this c=a+a; it seems to be a statement that must be executed in the body of the function, and then see this sentence:

Assignment statements need to have function execution, C language the most basic execution body is a function, you can initialize, but not in the function in vitro assignment.

in VC + +, there is a mistake, the pointer place error, error hint:


the error is that initialization must be a constant.

then I'll get rid of the C of the operation. As shown in Figure Program 2:


shows how it can function correctly. Then more depressed, asked QQ friends (Matrix). The correct answer is as follows:

Functions can only be initialized in vitro and cannot be assigned to operations. As shown in Figure Program 3:


the error is: variable A is repeatedly assigned an initial value.

in fact, the correct understanding is that the function can only be initialized in vitro, can not be assigned operations. Why the second program can execute.

Because, the C language execution process, the code is optimized, the program 2 in the int A;  a=10; Optimization for one sentence: int a=10; This is initialization. So program 2 is correct, program 3, initialization process: int a=10;int a=17; there must be a mistake. Duplicate initialization. In Program 1, the statement must be executed in the body of the function, which can only be initialized outside of the function.


Part Two global variables and local variables

Defined:

locals (local Variable): variables defined within the function body, scoped to the function body. Leaving the function body is invalid. Calling again is an error.

Global Variable: definition: A variable defined externally by all functions, scoped to the entire program, that is, all source files, including. C and. h files.

Example of a global variable, as shown in Figure Program 4:

The error of the hint is that the variable x is not defined because:

The C language code is executed in turn in the past, because the x definition is invalid in func1 () after the function func1 (), and the modifier is as follows, program 5:



as you can see, the output is the correct answer, so the global variable must be defined before the function body we need to use it.

Then, look at the local variable:


Precautions :

① variables defined in the main function are also local variables that can only be used in the main function, and variables defined in other functions cannot be used in the main function. The main function is also a function, equal to other functions.

The ②-shaped parametric and the variables defined in the function body are local variables, and the process of the argument giving the parameter values is the process of assigning values to local variables.

③ can use the same variable names in different functions, they represent different data, allocate different memory, complement each other, and do not get confused.

④ can also define a variable in a statement block (several statements contained by a pair of {}), and its scope is limited to the current statement block.


A comprehensive exercises:



① First Call FUN1 (), in fun1 (), there is a local variable n, so the output result is n:20;

② for Fun2 (), the use of formal parameters, the scope of the parameters are also the entire function (the actual parameter to the parameter value of the process is the process of assigning values to local variables), so the output of the result is n:30;

③ for Fun3 (), there are no local variables, so the n defined by the global variable is visible to all functions. So the result of output is n:10;

④ According to local variables note fourth. So the result of output is n:40;

The ⑤3c language stipulates that a variable can be found only from a small scope to a large scope, but not in the reverse, by using a variable in a smaller scope. For the main () function, even though the N in the code block is closer to the output statement, it still uses the n defined at the beginning of the main () function, so the output is 30.


So the result is:



Part III Storage of memory partitions and variables


Storage space for users in memory is divided into three parts: program area, static storage area, dynamic storage area.

Categories of variables:

① are allocated from space: The scope of the variable can be divided into local and global variables.

② can be divided into static storage and dynamic storage from the existence time (life cycle) of the variable.

Static storage: A program allocates a fixed storage mode during run time.

Dynamic Storage: Programs dynamically allocate storage space as needed during run time.

Global variables are all stored in the static storage area. allocate space to global variables as the program begins to execute, releasing space when the program is finished. A fixed allocation space during program execution without the need for dynamic allocation is released.

The dynamic storage area mainly holds the following data: function parameters, automatic variables (without static local variables), field protection and return values of function calls.

Dynamic store data that allocates dynamic storage space at the start of a function call and frees the space at the end of the function.

Each variable in the C language has two properties: the first data type, and the second storage property.

Auto variable:

The local variables in the function, if not specifically declared as static storage, are dynamically allocated storage space. The data is stored in a dynamic storage area. The parameters in a function and the variables in the function belong to this class, the system automatically allocates space when the function is invoked, and the space is freed at the end of the function. These local variables are called automatic variables. Automatic variables are declared with the Auto keyword (you can omit auto).

Static variable:

Sometimes it is hoped that the function we call will not disappear when it is released to preserve the original value, which needs to be defined as a static local variable. Use the keyword static declaration.
Look at a program:


Description of static local variables:
1 static local variables belong to the static storage category, the storage unit is allocated in the static storage area. is not released during the entire run of the program. But the automatic variable (that is, dynamic local variable) belongs to the dynamic storage category, which occupies the dynamic storage space, and the function call is released after the end.
2 static local variables at compile-time initial value, that is, only once the initial value, and the initial value of the automatic variable is in the function call, every time the function is called again to the initial value, equivalent to the execution of an assignment statement.
3 If the local variable is defined without an initial value, then for static local variables, the compile-time automatically assigns an initial value of 0 (for numeric variables) or null characters (for character variables). For an automatic variable, its value is an indeterminate value if it is not assigned an initial number.

Register variable
In order to improve efficiency, C language allows local variables to be placed in the CPU registers, this variable is called "register variable", with the keyword register as a statement.

Description
1 only local automatic variables and formal parameters can be used as register variables;
2 The number of registers in a computer system is limited and can not define any number of register variables;
3 Local static variables cannot be defined as register variables.


Declaring external variables with extern
An external variable (that is, a global variable) is defined outside of the function, and its scope starts at the variable definition and ends at the end of this program file. if the external variable is not defined at the beginning of the file, its valid scope is limited to the end of the file at the definition. If the function before the definition point wants to refer to the external variable, you should use the keyword extern to declare the variable as an external variable before referencing it. Indicates that the variable is an external variable that has already been defined. With this declaration, you can legitimately use the external variable from the declaration.

extern example:


Variables defined outside the body of the main function, A, B (external variable: its scope begins at the variable definition, to the end of this program file). )。 To reference in main, you must add a keyword: extern.


Part III: Heap and stack of C language

The first difference with the stack is that the application is different: the stack (English name is stack) is the system allocates space automatically, for example we define a char a; the system automatically opens up space on the stack. and the heap (English name is heap) is the space that the programmer applies to oneself according to need, for example malloc (Ten), Open 10 bytes of space. Because the space on the stack is automatically recycled, so the data on the stack life cycle is only in the function of the running process, after the release, can not be accessed. The data on the heap can be accessed as long as the programmer does not release the space, but the disadvantage is that once the release is forgotten it will result in a memory leak.






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.