A keyword for C language--static

Source: Internet
Author: User

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

1. Static modifier variable

Variables are divided into local variables and global variables, depending on the scope of the action . If you modify a variable with static, whether the variable is global or local, it is stored in the static data area . In the following separate terms:

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

The main purpose of modifying a global variable with static is to make its scope limited to the file defined by the variable (that is, from the definition of the variable to the end of the 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.

We use static to modify the local variables for the purpose of the main two:

1) Static local variables defined within a function body can only be accessed in the body of the function, even if 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 this static local variable will not be destroyed, and the function will still use this value the next time it is used.

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:

The

2,1

3,1

4,1

5,1

6,1

7,1

8,1

9,1

10,1

From the running result of the routine, we can see:

For fun1 (), after a run, the variable I retains the original data and is not destroyed, but continues to be used at the next call, so there will be 1, 2, 3 ... 10 such a result. For Fun2 (), after a run, the value of the variable j is destroyed, so the second call to end J still retains the original value "1", so the loop 10 times, each time is 1

2. Static modifier function

Add static before the function, this function becomes a static function (intrinsic function), the main purpose of our 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 be accessed by other files). This is a benefit: programmers don't have to worry about having their own functions with the same name as other files .

Special description of local static variables

(1) Static local variable data belongs to the static storage class, allocating memory units within the static storage area, and is not released during the whole program operation. The automatic variable (i.e. dynamic local variable) belongs to the dynamic storage class, which occupies the dynamic storage space without occupying the static storage space, and frees up the space immediately after the function call is finished.

(2) When a static local variable is assigned at compile time , that is, it is assigned only once , and it already has an initial value when the program runs. 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 . The automatic variables are assigned the initial value, not at compile time, but at runtime , so each call to the function assigns an initial value.

(3) If the local variable is not assigned to the initial value, then for the static local variable, the compilation automatically assigns an initial value of 0 (to the numeric type variable) or null character (to the character variable). in the case of an automatic variable, if the initial value is not assigned, it is an indeterminate value. This is determined by the different memory units that are allocated dynamically each time.

    1. Static first effect: Hide

      When we compile multiple files at the same time, all global variables and functions that do not have a static prefix have global visibility.

    2. The second function of static is to keep the contents of the variable persistent. Variables stored in the static data area are initialized at the start of the program and are the only one initialized.

    3. The third function of static is to initialize to 0 by default. In fact, global variables also have this property, because global variables are also stored in the static data area. In the static data area, all bytes in memory default values are 0x00, sometimes this feature can reduce the workload of the programmer.

reference:http://blog.csdn.net/21aspnet/article/details/1535573

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.