The difference between function and normal function in C language with static modifier

Source: Internet
Author: User

The C program has been composed of the following parts:
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 the information (return address; Environment information) needed to be saved each time the function is called.
5) heap--dynamic storage points.

The global variable is defined as a global static variable by adding the keyword static before the global variable.
1) in-memory location: static storage (static storage is present during the entire program run)
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the 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.

Benefits:
Benefits of defining global static variables:
<1> will not be accessed by other files, modify
<2> other files can use variables of the same name, and no conflicts will occur.

Local static variables
The local variable is defined as a local static variable by adding the keyword static before the local variable.
1) in-memory location: Static Storage Area
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: The scope is still a local scope, and when the function or statement block defining it ends, the scope ends.

Note: When static is used to modify a local variable, it changes the location where the local variable is stored and changes from the original stack to a static storage area. But the local static variable is not destroyed after it leaves the scope, but still resides in memory until the program is finished, but we can no longer access it.
When static is used to modify a global variable, it changes the scope of the global variable (which is invisible outside of declaring his file), but does not change its location or in the static store.

3. Static functions
The function is defined as a static function by adding the keyword static before 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.
Benefits of defining static functions:
<1> other files can define the same name function, no conflict
<2> static functions cannot be used by other files. Storage descriptor auto,register,extern,static, corresponding to two storage periods: 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. Local variables declared with static have a static storage duration (static storage duration), or a static range (static extent). 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.
Because of the above characteristics of the static variable, some specific functions can be implemented.
1. Number of statistics function
Declare a local variable of the function and set it to the static type as a counter so that the function can be counted each time it is called.  This is the best way to count the number of functions being called, because this variable is closely related to the function, and the function may be called in many different places, so it is difficult to count from the caller's point of view. The benefits of using static functions in C: Static functions are automatically assigned to a storage area that is always in use until the application instance is exited, and the stack is faster when the function is called.
The keyword "static", translated into Chinese, is "still", so the internal function is also called the static function. But the meaning of "static" here does not mean storage, but the scope of the function is limited to this file. The advantage of using intrinsic functions is that when different people write different functions, they don't have to worry about the functions they define, or whether they have the same name as the functions in other files, because the same name doesn't matter.
The semantic 1.static variable for static in C: 1). local A. Static local variables are defined within a function and have a lifetime of the entire source program, but the scope is the same as the automatic variable, and can only be used within the function that defines the variable. After exiting the function, it cannot be used even though the variable continues to exist. B. The system automatically assigns a 0 value to a static local variable of a basic type if it is not assigned the initial values in the description. The value of the automatic variable is indeterminate if it is not assigned the initial values. 2). Global global variables are statically stored in their own right, and static global variables are, of course, stored in a static manner. But their scope, the scope of the non-static global variable is the whole source program (multiple source files can be used together), while the static global variable restricts its scope, that is, only in the source file that defines the variable, it is not available in other source files of the same source program. A 2.static function (also called an intrinsic function) can only be called by a function in this file, not by a function in another file in the same program. Unlike general non-static functions (external functions) static in C can be used to modify variables, can also be used to modify the function. First look at the time to modify the variables. Variables in C can be divided into existing global data regions, stacks and heaps. In fact, we usually say that the stack is a stack and does not contain the right, do not confuse.

The difference between function and normal function in C language with static modifier

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.