c keyword Register, extern, static, some summary, and the use of the project experience

Source: Internet
Author: User

First, two concepts are introduced:

The life cycle of a variable:

The lifetime of a variable from the time period established to the undo. Static variables, from the generation of variables to the end of the entire program execution. When a function uses a variable to end, the variable's storage space still exists, and the value of the variable will change as the function uses it. If a static variable is defined within a function, the value of the variable will be preserved the first time it is called, and the value of the variable is the value at the end of the first call when it is called the second time. Dynamic variables, as the name implies, the storage space of a variable is generated and revoked dynamically with the invocation of the function. For example, when a dynamic variable is defined in a function, the period of the variable is set from the variable to the end of the function, not the end of the program.

Second, the scope of the variable:

The scope of a variable is the scope of the variable that can be referenced in the program, and the extent to which the variable is available is scoped. The scope and life cycle of a variable are different concepts.

1. Automatic variable (autovariable)

Local variables in the function, if not specifically described by static, the compilation system defaults to an automatic variable, which allocates memory space in function calls, the data is stored in the dynamic store, and the space is freed automatically after the function call ends.

2. Register

In general, variables are stored in memory, and when they are used in the program, they are taken out of memory and sent to the CPU. For frequently used variables, such as for loop operations, to increase efficiency, you can request the compiler to save the variable in the CPU register, which is the register variable, which speeds up the program. But now the compiler is more optimized, so you don't have to declare variables with register.

3. Static

(1) Static and variable

Static and local variables

In the function of a local variable, plus the keyword static, the variable is defined as a static local variable. If the identifier is declared static, it has a static life cycle. It always resides in the global data area until the program finishes running. But its scope is a local scope, and when the function or block of statements that defines it ends, its scope ends. Therefore, after the execution of the function, the storage space for the variable is still present, and it is not undone with the function execution, but other functions cannot use it, or other functions cannot "see" it. Static local variables are only assigned an initial value at compile time, and if no initial value is assigned to the variable, it is automatically assigned 0 at compile time, and the value of the variable remains at the end of the last use when the function is called the next time.

Static and global variables

Before the global variable, the variable is defined as a static global variable, plus the keyword static. At this point, only variables can be used in this file, and other files cannot be used.

For a global variable, it is stored statically, even if it is not static before the definition, and static only affects the scope of the variable, so you can use the variable in another file.

(2) Static and function

Adding static to the function means that this function can only be called in this file, and the other file "cannot see" it. This makes it possible to use the same function names in two files without affecting each other.

(3) Static and class

Static and data members of a class

1) for a data member of a class, preceded by the static keyword, which represents the data member that is not part of an object, but which is accessible to all objects. Also, you can access static data members by using the object (. operator) as normal data members, or by using the class name (:: operator) in a proprietary way.

2) static data members account for only one copy of the memory space, so each object can change its value. Its storage space is not revoked with the object being set up, but is revoked at the end of the program's run.

3) A static data member cannot be initialized with a parameter list, and if it is not initialized, the sub-compilation system initializes it to 0.

4) Instead of static data members, each object is generated with a storage space that generates the data.

Static and member functions of a class

1) Like a static data member, a static member function is also a class, not an object, so it can be accessed through an object (. operator), or through a class name (:: operator) to access a static member function.

2) because static member functions belong to a class and do not belong to a particular object, and the this pointer is about a particular object, the static member function does not have the this pointer, and static member functions are generally used to access static data members without accessing non-static data members (which can access non-static data members, but do not advocate doing so). Instead of a static shared (public) member function, you can access both static data members and non-static data members.

4. extern

extern can only be used to declare a defined variable and cannot be used for the definition of a variable.

A declaration includes a definition, but not all declarations are definitions. A simple method of distinguishing between declarations and definitions can be used: a declaration that establishes a storage space is called a declarative declaration, and a declaration that does not establish a storage space is referred to as a declarative statement.

int A; A declaration of a definition is both a declaration and a definition. Opening up storage space for variable a

extern int A; Just declaring, not defining, not opening up storage space for variable a

(1) using extern within a file

The scope of the variable, usually starting from the definition of the variable, if you want to use this variable before defining the variable, you can declare an external variable to extend its scope, that is, within the same file, after the extern declaration, the scope extends to the end of the file. Declare this variable with extern, where the variable declaration indicates that the variable is already defined elsewhere, that the declaration does not produce a new variable and no longer allocates memory space to the variable.

(2) use extern within multiple files

Believe that many programs small white will encounter in a number of files compiled together, want to call a variable but there are various error situations, then only need to master the usage of extern can solve the problem.

For multiple source files, if you want to use another defined external variable in one file, you can use the extern declaration in a file that does not define the external variable, and if you encounter extern at compile time, the external variable is first found within the file. If not found, it is found in other files at link time. If a variable with the same name is defined in two files, a duplicate-defined error occurs. However, it is important to note that extern declarations of variables in different files can affect each other, and the value of the global variable may be changed when a file is executed, affecting the invocation of other files. If we want this external variable to be used only within this file, but not by other file references, you can add a static declaration when the external variable is defined. Prevent misuse of modules written by others. That is, a static int A is used in a file, and it is not possible to attempt to extend the scope of a with extern int A in another file, which can be compiled, but not run. Remove static and the program can run.

Experience: These C language modifier variables of the keyword, in writing the project is of great use, although in many cases, do not use them will not affect the operation of the program, but as a good programmer, we need to master more effective tools, write good projects, we need to master these basic skills. Only by mastering more skills or programming experience can you write better, more portable programs to become a better self-cultivation programmer. In short, in the actual combat constantly improve themselves, encountered problems, to continue to learn, and solve these problems, the level will be better to improve!

c keyword Register, extern, static, some summary, and the use of the project experience

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.