C language learning notes: 12 _ variable storage method and storage period, language learning variables

Source: Internet
Author: User

C language learning notes: 12 _ variable storage method and storage period, language learning variables

/** Storage method and storage period of the 12_variable. c ** Created on: July 5, 2015 * Author: zhong */# include <stdio. h> # include <stdlib. h>/*** scope of the sub-variable: global variable, local variable * Time (lifecycle) of the existence of the sub-variable: static storage method, dynamic Storage ** static storage mode: The system allocates a fixed storage space during the running of the Program * dynamic storage mode: dynamically allocates storage space as needed during the running of the program. ** Data storage location: static storage zone and dynamic storage zone ** 1: global variables are all stored in the static storage zone. When the program starts to run, global variables are allocated to the storage zone, release after completion, occupying a fixed storage unit during execution, rather than dynamically allocating and releasing. * 2: Dynamic Storage zone: format parameters, variables defined in functions (without static keywords), on-site protection and return addresses for function calls * storage units are allocated in the dynamic storage zone, it will not be released during the entire running of the program. ** In C, each variable and function has two attributes: Data Type (such as int and float) and data storage type (static and dynamic storage ). ** There are four storage classes in C: Automatic (auto, default), static (static), register (register), and external (extern) * different storage classes, the scope and lifetime of variables are also different. ** ----------------------------------------- * 1: Storage Class of local variables: * 1: Automatic variables (default): local variables in functions. If they are not specifically named static, the storage space is dynamically allocated, and the data is stored in the dynamic storage area. * When a function is called, a bucket is allocated to these variables, and the bucket is automatically released at the end. * For example, the parameter of the function and the local variable. * Auto variables use the keyword auto to declare storage classes. ** Int f (int a) {// parameter * auto int B, c = 3; // int B, c = 3; automatic local variable, of course, auto can be omitted (which we usually omit) **} ** 2: static local variable (static local variable): only the local variable int; the preceding static keyword static int a is added. The data is stored in the static storage area. * Feature: When the function ends, the storage unit of the static local variable will not be released. When you call this function next time, this value is the value generated when the last call ends. ** Comparison between static local variables and automatic local variables: * 1: static local variables assign initial values only once during compilation. They already have initial values when the program is running, in the future, the initial values will not be re-assigned when the function is called, but the value at the end of the last function call will be retained * 2: the initial values of dynamic local variables are not implemented during compilation, it is implemented when a function is called. Each call is performed, and the function is re-proportional to the initial value. * 3: If an initial value is not assigned when a static local variable is defined, the system automatically assigns the initial value 0 or an empty character '\ 0 '. For automatic variables, if the initial value is not assigned during definition, the system will randomly allocate an uncertain storage unit, so the value is uncertain. * 4: static local variables still exist after function call, but cannot be referenced by other functions. ** Use Cases of static local variables: the value at the end of the last call of the function needs to be retained * For example, the factorial value from 1 to 5 x int fac (int n) {* static int fac = 1; * fac = fac * n; * return fac; *} ** int main () {* int I; * for (I = 1; I <= 5; I ++) * printf ("% d! = % D \ n ", I, fac (I); *} ** 3: register variable (register): Save the variable to the cpu register. * Note: * values of common variables (static and dynamic storage) are stored in the memory, when the program uses the "controller" command to send the value of this variable in the memory to the "handler" for calculation, * after the operation, the data is sent from the handler to the memory for storage. ** Why do we need to operate register variables? * For example, when a function executes 1000 cycles, local variables must be referenced in each loop (frequent access to a variable ), if the "controller" sends a command to "access" variable values from the memory each time, it will take a lot of time. * Place the value of the local variable in the cpu register. The time period is used to retrieve the value from the register for calculation, and you do not need to access the value from the memory. Fast. ** Declare register variables: register int f; ** due to computer development, the optimized compilation system can automatically identify frequently used variables and put them in registers. In actual programming, programmers do not need to specify this variable too much. ** ------------------------------- * 2: Storage Class of global variables: ** global variables are stored in static storage areas, so their lifetime is fixed. The entire process of running the program. * Although the lifecycle is fixed, the scope of global variables is uncertain. Different storage classes have different scopes. ** Generally, if the global variable (external variable) is not defined at the beginning of the file (the entire source file), its scope starts at the definition and ends at the end of the program file. Functions before definition cannot be used. ** If the global variable is not defined by the file switch and you want to use the global variable in the previous function, you need to extend the scope of the global variable. ** 1: extend the scope (extern) of the external variable in a file * When the function references the external variable defined after the function, make the keyword extern to make an "external variable Declaration" for the variable ": extends the scope of external variables to this location. * For example, * int fun () {* extern int; // extern: make an "external variable Declaration" for this variable "*//... the statement after this function can use this external variable *} * int A = 20; // The external variable named after the function ** 2: extend the scope of external variables to other files * extend the global variables in other source files to this source file. Add an extern keyword. ** Example: * file 1.c: * int A = 20; * int max () {* A = 3; *} ** file2.c: * int funk () {* extern int A; // extend the global variable a defined in file 1.c to this file and use it as A local variable. int can be omitted * A = 34; *} * extern A; // extend the global variable a defined in file 1.c to this file as an external variable of this file, and the function after this is available, the previous function cannot be used * int funk () {* A = 3; // The function after this is available *} ** (static external variable) * 3: restrict the scope of the external variables in this file (do not allow access from other files): You only need to add the static keyword when defining the external variables. * At the same time, the name of the global variable with the static keyword is added. Other files can also use this file without conflict. ** static int A = 20; // In this way, other files cannot access this external variable. * Int main () {**} *** conclusion: * 1: The Role of a variable declared in static mode: * 1: use static statements for local variables, allocate it to the static storage zone. This variable will not be released during the entire program execution * 2: use static Declaration for global variables, so the scope of this variable is limited to this file module, other files cannot be referenced ** 2: declaring the role of the storage type * for local variables, declaring the storage type is used to specify the region where the variables are stored (static storage, dynamic storage, registers ), and the resulting Lifetime issues * for global variables, because the memory allocated during compilation is stored in the static storage area, the purpose of declaring the storage type is to expand the scope of the variables, none. * ** // Local static, dynamic Variable void local_static_auto (int a) {auto int B = 1; // auto can omit the automatic variable static int c = 3; // define static variables. during the running of the program, the storage unit will not be released. This statement will only be executed once during compilation and will not be executed again after running. // When the second call is made, the initial value 3 will not be assigned again. Only the value printf ("% d run: B = % d, c = % d, a + B + c = % d \ n ", a, B, c, (a + B + c); c ++; // c is automatically added and stored in the storage unit, so that the function is used for the next call. B ++; // auto variables are useless after auto-increment here, Because} void test_local_static_auto () {int I; for (I = 1; I <5; I ++) {local_static_auto (I);}/*** output: 1st run: B = 1, c = 3, a + B + c = 5 2nd run: B = 1, c = 4, a + B + c = 7 3rd run: B = 1, c = 5, a + B + c = 9 4th run: B = 1, c = 6, a + B + c = 11 */} // calculate the factorial int fac (int n) {static int fac = 1; // record the product result fac * = N; return fac;} void test_fac () {int I; for (I = 1; I <= 5; I ++) printf ("% d! = % D \ n ", I, fac (I);/* 1! = 12! = 23! = 64! = 245! = 120 */} extern a; // returns the 11_global variable. the global variable a defined in c is extended to this file, and the subsequent functions are available. The previous functions are unavailable. // The external Variable void use_extern_variable () defined after this function is used () {extern int A; // extend the scope of external variable a to start from here. // extern A; // The type name can be written but cannot be written as A = 30; printf ("extern_variable A = % d, other file a = % d \ n", A, a) ;}int A = 20; int main12 () {// test_local_static_auto (); // test_fac (); use_extern_variable (); extern a; // set the 11_global variable. the global variable a defined in c is extended to this method. Other methods cannot use printf ("% d", a); return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.