The role of the static keyword in C language

Source: Internet
Author: User

The role of the static keyword in C language

Huaqing Vision 2014-10-05 Zhang Junhao, Haidian District, Beijing

One, C source program compiled executable (ELF) storage space layout


1) The machine instruction part that the body section--CPU executes; a program has only one copy; read-only to prevent the program from modifying its own instructions due to an accident;

2) Initialize the data segment (data segment)--All the global variables assigned the initial value in the program, stored here.

3) Non-initialized data segment (BSS segment)--global variable not initialized in the program; the kernel initializes this segment to 0.

4) Stack-growth direction: top-down growth, automatic variables and information to be saved each time the function is called (return

Address, local variables, environment information).

5) heap--dynamic storage points. Is the data type that extends to the high address, which is the bottom-up extension.

Storage specifier auto,register,extern,static, Corresponds to two storage period modes: Automatic storage period and static storage period. Auto and register correspond to automatic storage periods. A variable with an automatic storage period is established when it enters the block that declares the variable, it exists when the block is active, and is revoked when it exits the block.

The keyword extern and static are used to describe variables and functions that have a static storage period. A local variable declared with static has a static storage duration (staticstorage duration), or a static range (staticallyextent ). Although his value remains valid between function calls, the visibility of its name is still limited to its local domain. A static local object is first initialized when the program executes to the declaration of the object

Second, the life cycle

(1) Local Variables allocate space at each function call and are destroyed after the function call ends.

(2) A global variable allocates space when the program starts running and is destroyed at the end of the program.

(3) if local variables and global variables names , local variables temporarily overwrite global variables.

(4) Global variables can only be used with constant expressions initialization , since the program starts running with the appropriate values to initialize the global variables, the initial values must be saved in the compiled executable, so the initial values are calculated at compile time. C language is syntactically defined, and global variables can only be used constant-expression initialization .

The role of the static keyword in c language

variables for "1"static action

1.static modifying Global variables: add static before global variables , and global variables are defined as a global static variable.

Features are as follows:

1) store (in-memory location): Static storage is not changed (static storage is present during the whole program operation);

2) initialization : Uninitialized global static variables are automatically initialized to 0by the program. (The value of an automatic object is arbitrary unless he is displayed initialized)

3) scope : Global static variables are not visible outside the declaration of his file. Start with the definition exactly to the end of the file. The scope of a non-static global variable is the entire source program (multiple source files can be used together), while a static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Benefits:

1) will not be accessed by other documents, modified;

2) variables of the same name can be used in other files and no conflicts will occur.

2.static modifying a local variable: By adding the keyword staticbefore the local variable, the local variable is defined as a local static variable. Features are as follows :

1) store (in-memory location): from stack to static storage rodata, lifetime is the entire source program and can only be used within the function that defines the variable. After exiting the function, although the variable continues to exist, it cannot be used ;

2) initialization : assigning the initial value to a static local variable is done at compile time, that is, the initial value is assigned only once , and it has an initial value when the program is running. The initial value is no longer re-assigned at each subsequent call to the function, but only at the end of the last function call. Instead of assigning an initial value to an automatic variable, not at compile time, it is done at the time of the function call, and each call to the function is given again, relative to the execution of the assignment statement. If the local variable is not assigned the initial value , for the static local variable, the compiler automatically assigns the initial value 0(to the numeric type variable) or the null character (to the character type variable). In the case of an automatic variable, if the initial value is not assigned, it is an indeterminate value. This is because the storage unit has been disposed after each function call, and the storage unit is reassigned the next time it is called, and the value in the allocated cell is indeterminate.

3) scope: The scope is still a local scope, and when the function or block of statements that defines it ends, the scope ends, although the variable continues to exist, but it cannot be used.

function of "2"static function

The function is defined as a static function by adding the keyword staticbefore the return type of the function.

The definition and declaration of a function is extern by default , but the static function is only visible in the file that declares him and cannot be used by other files. Can be called only by functions in this file, not by functions in other files in the same program. Benefits:

1) functions with the same name can be defined in other files and no conflicts will occur

2) static functions cannot be used by other files.

The difference between variables, functions and ordinary variables and functions ofstatic function

The variable of "1"static action differs from normal variable and function

1) What is the difference between a static global variable and a normal global variable ?

A static global variable is formed by the description of the global variable ( external variable ) , preceded by static .

Global variables themselves are static storage , and static global variables are, of course, static storage methods. The two are not different in how they are stored.

The difference between the two is that the scope of the non-static global variable is the entire source program , when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file , it can be common only for functions within that source file, so you avoid causing errors in other source files.

The static global variable is only initialized once, preventing it from being referenced in other file units ;

2) What is the difference between a static local variable and a normal local variable?

Changing a local variable to a static variable changes the way it is stored, which changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use .

The static local variable is initialized only once, the next time based on the last result value;

What is the difference between the function of the "2"static and the normal function?

The static function differs from the normal function scope only in this file. Functions that are used only in the current source file should be described as intrinsic functions (static decorated functions ), and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to use these functions is to contain the header file . The static function has only one copy in memory, and the normal function maintains a copy of each call .

The role of the static keyword in C language

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.