The meaning of the static modifier in C language

Source: Internet
Author: User

In c, static usually has 2 Meanings: 1 defines the life cycle of a variable, and 2 defines the scope of a variable or Function.

The life cycle of a variable is the period of time that a variable exists, relative to the process life cycle that the program is Running. The life cycle of a variable is determined by the storage type (location) of the Variable. therefore, the 1th meaning of static can also be understood as static, which defines the storage type (location) of the Variable.

In c language, variables are stored in stacks, global static zones, and HEAPS. The stack is used for function call, parameter passing and so on, which is the most frequent data area in the process of running the Program. The global static zone, which is a variable that persists in memory, is not destroyed during the entire operation of the Program. heap, is a large space memory expansion area maintained by the operating system, the process can actively request to the operating system to use the heap of storage, in the process of running a dynamic Application.

A variable modified by static whose storage type is a global static zone, and the variable is not destroyed during the entire run of the program and is initialized only 1 times.

Code 1:static The life cycle of the modified variable (storage Type)

voidfunc1 () {Static intVAR1 =1;//allocating variable var1 storage space in the global static zone    intVAR2 =1;//allocating VAR2 storage space on the stackvar1= var1+1; Var2= var2+1; printf ("var1=%d, var2=%d", var1, var2)}voidmain () {func1 ();    Func1 (); Func1 ();}

Code output:

"var1=2, var2=2"

"var1=3, var2=2"

"var1=4, var2=2"

Scope refers to the visible area of a variable/function, which can be referenced by a Region. In the C language, functions that are not static decorated are visible to all source files, and global variables without static adornments are visible to all source Files. by static modification, The scope of the function can be limited to the source file, and the scope of the global variable is limited to the source File.

Because global variables and functions are scoped to all source files, all source files refer to the same global variable and function, so the C language does not allow the same global variables and functions to be defined within 2 source files, and the link error is prompted during the code link phase. By adding a static adornment, you can have 2 source files define the same global variables and functions, but note that the global variables and functions referenced by these 2 source files are Independent.

Static common use occasions

Static int 1;       // limit global variable var1 only visible in source file Static void func1 ()        //  qualification function only visible in source file {    staticint VAR2 =1;     // Var2 is a static variable, not a stack variable, and is initialized only once }       void  main () {    func1 ();}

The meaning of the static modifier in C language

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.