What is the difference between a static function in C and a normal function in addition to being called outside?

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.

Http://t.qq.com/wfniupixuan
http://user.qzone.qq.com/3054539871
http://user.qzone.qq.com/3054539871/blog/1430363505
http://user.qzone.qq.com/3054539871/blog/1430362794
http://www.lwinfo.com/uzt/list1/273596.html
http://www.lwinfo.com/uzt/list1/273597.html
http://www.lwinfo.com/uzt/list1/273599.html
http://www.lwinfo.com/uzt/list1/273600.html
http://www.lwinfo.com/uzt/list1/273601.html
http://www.lwinfo.com/uzt/list1/273602.html
http://www.lznews.gov.cn/uzt/list1/273603.html
http://www.lwinfo.com/uzt/list1/273604.html
http://www.lwinfo.com/uzt/list1/273606.html
http://www.lznews.gov.cn/uzt/list1/273607.html
http://www.lwinfo.com/uzt/list1/273608.html
http://www.lznews.gov.cn/uzt/list1/273609.html
http://www.51etong.com/jkzt/list1/273610.html
http://www.lwinfo.com/uzt/list1/273611.html
http://www.lwinfo.com/uzt/list1/273612.html
http://www.lznews.gov.cn/uzt/list1/273614.html

What is the difference between a static function in C and a normal function in addition to being called outside?

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.