A keyword in C language--static

Source: Internet
Author: User
Tags modifier

a keyword in c language--static

Static in C language has two functions, the first is to modify the variable , the second is the modifier function .

1. Static modifier variable

According to the scope of action, the variables are divided into local variables and global variables. If you modify a variable with static, whether the variable is global or local is stored in the static data area . The following separately:

¨ If you modify a global variable with static, we call it a static global variable.

The primary purpose of modifying a global variable with static is to make its scope limited to a variable-defined file (that is, from the definition of the variable to the end of this file), and no other file can be accessed by any means.

¨ If you modify a local variable with static, we call it a static local variable.

There are two main purposes for modifying local variables with static:

1 static local variables defined within a function body can only be accessed in this function body, even if the other functions of the same file are not accessible.

2 Static local variables are always stored in the static data area, so even if the function ends, the value of the static local variable will not be destroyed, the function will still use this value the next time.

 

a routine is given below:


#include <stdio.h>

static int J; //Static global variables

int fun1 (void)

{

static int i = 0;

i++;

return i;

}

int fun2 (void)

{

j = 0;

j + +;

Return J;

}

int main (int argc, char *argv[])

{

int k = 0, m = 0, n = 0;

For (k=0 k<10; k++) {

m = fun1 ();

n = fun2 ();

printf ("%d,%d\n", M, n);

}

return 0;

}

Results:

1,1

2,1

3,1

4,1

5,1

6,1

7,1

8,1

9,1

10,1

 

you can see from the running results of routines that:

For fun1 (), after running once, the variable I kept the original data, and did not destroy, but in the next call to continue to use, so there will be 1, 2, 3 ... 10 such a result. and for Fun2 (), after the run, the value of the variable j is destroyed, so the second call to end J still keep the original value "1", so the loop 10 times, each time is 1

 

2. Static modifier function

When you add static to a function, the function becomes a static function (an intrinsic function), and the main purpose of the static modifier is to represent a function that cannot be accessed by another file (as with the purpose of modifying a global variable with static), are not allowed to access other files. so there's a benefit: programmers don't have to worry about writing functions with the same name as other files .

 

 

 

Special description of local static variables

(1) The static local variable data belongs to the static storage category, allocates the memory unit in the static storage area, does not release during the entire program operation. But the automatic variable (that is, dynamic local variable) belongs to the dynamic storage category, occupies the dynamic storage space without occupying the static storage space, and immediately frees up the space after the function call ends.

(2) When a static local variable is assigned at compile time , that is, the value is assigned only once at a time , and it already has initial values when the program is running. each time the function is called, the initial value is no longer assigned, but only the last time the function call was left . The initial value of an automatic variable is not performed at compile time, but at run time , so the initial value is assigned once for each function call.

(3) If the local variable is defined without an initial value, for static local variables, the compile-time automatically assigns an initial value of 0 (for numeric variables) or null characters (for character variables). in the case of an automatic variable, the value is an indeterminate value if the initial values are not assigned. This is determined by the different memory units that are dynamically allocated each time.

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.