Talk C Chestnut Bar (126th: C language instance--static keyword)

Source: Internet
Author: User

Crossing, hello, everyone. The example of the built-in macro we said last time, this time we say the example is: Static keyword. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, the C language provides the static keyword, which often appears at the top of a variable or function, why add it? What role does it have? Today we are going to learn about the static keyword.

Before the introduction, let's do some groundwork. It mainly introduces the life cycle and scope of variables or functions.

The life cycle of variables and functions

The so-called life cycle refers to the time that a variable or function can be used in a program, which is a period of time, possibly a minute or an hour. The life cycle usually starts with the time the variable is used, until the end of the variable is used, and the time in this time period is called the life cycle of the variable. This is a bit like the spring and autumn seasons of nature, which begin every year, until the end of winter. From spring to winter this process is a cycle, but we are accustomed to call this cycle for one year.

The variables in C language have global variables and local variables, and the life cycle of global variables is the same as the life cycle of the program, which means that global variables run through the whole procedure. The life cycle of a local variable is not so long, it only begins to appear when it needs to be used, and when it is finished, it completes its mission to end life. The most common are the variables in the function, which are local variables that begin as the function runs, and they disappear when the function finishes.

Let's take a practical example to illustrate the life cycle of a variable:

intGloble_value =1; void Func () {intLocal_value =3;printf("Globle_value = %d \ n", Globle_value);//Global variables can be used in this functionprintf("Local_value = %d \ n", Local_value);//Local variables can be used in this function}intMain () {func ();printf("Globle_value = %d \ n", Globle_value);//Global variables can be used in the main function//printf("Local_value = %d \ n", Local_value);//Local variables cannot be used in the main functionreturn 0;}

We save the above code to a file, and compile the file, and then run the compiled program, the program running results are as follows:

1    //在func函数中可以使用全局变量3     //在func函数中可以使用全局变量 1    //在main函数中只能使用全局变量

As you can see from the example above, global variables can be used in both the custom Func function and the main function, because its life cycle is the same as the program's life cycle. Local variables can only be used in the Func function, not in the main function, and if the annotations in the program are removed, a compilation error of "Variable undefined" is generated. The reason is that local variables end with the end of the Func function, so local variables are not visible in the main function.

Scope of variables and functions

The scope of a variable represents the valid scope of the variable, and the variable can be used within a range that cannot be used outside of that range. For example, our daily life ID card, in the domestic can be used at will, but after going abroad can not be used, can only use passports. This is because the scope of the identity card is domestic, after going abroad to indicate beyond the scope of the identity card, then you can no longer use the identity card. We also use the example above to illustrate:

    • The global variable globle_value is scoped to the entire file, so it can be accessed by the Func function and the main function in the same file.
    • The scope of the local variable local_value is inside the function func. Therefore it can only be used within the Func function and cannot be used elsewhere because it is beyond its scope.

With these mats, we introduce the static keyword is much easier, thestatic keyword has two main functions: limit the variable or function life cycle and scope.

For a variable, static does not affect its life cycle, it simply initializes the uninitialized variable to 0. However, it will narrow the scope of the variable, which has no effect on the local variable, mainly to reduce the scope of the global variable to a file, for example, we in the above example, the global variable globle_value, which can be used in other files, if it uses the Static keyword qualification, It can only be used in the current file.

Here is a practical example, please refer to:

staticvoid func(){    int temp;    staticint static_local_value ;    printf("temp = %d \n"// default value is not known    printf("static_local_value = %d \n"// default value is 0}

We define a normal variable and a static modified variable in the function func, and none of them are initialized. The static variable is initialized to 0 at compile time without initializing the normal variable. The following is the result of the program operation, please refer to:

temp = 134513922 static_local_value = 0 

As you can see from the program running results above, the default value of the static variable is 0, and the default value of the normal variable is indeterminate.

For a function, static does not affect its life cycle, but it shrinks its scope, which is the same as the static-qualified variable, so I don't introduce it much.

Crossing, the complete code is put in my resources, you can click here to download the use.

In writing code, we often use static to limit variables and functions, primarily to narrow their scope to a file, which avoids naming conflicts with variables or functions with the same name in other files.

For example, it is said that there are many people in China called Zhang Wei, that is to say the same name. If I called Zhang Wei here, then there may only be a crossing called Zhang Wei, after all, listen to my novels here crossing the number of people is not many, if I send a post online said: Zhang Wei winning. Then there may be a lot of people called Zhang Wei to receive the prize, how to distinguish so many Zhang Wei, using the static keyword to limit the line, haha.

Crossing, here's an example of the static keyword. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (126th: C language instance--static keyword)

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.