Talk C chestnuts together (126th back: C language instance -- static keyword)

Source: Internet
Author: User

Talk C chestnuts together (126th back: C language instance -- static keyword)

Hello, everyone. The built-in macro example we mentioned in the previous article is static keyword. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

The C language provides the static keyword, which often appears at the beginning of a variable or function. Why should I add it? What is its main function? Today, let's take a look at the static keywords.

We will make some preparations before introduction. This section describes the lifecycle and scope of a variable or function.

Lifecycle of variables and functions

The so-called life cycle refers to the time that a variable or function can be used in a program. It is a period of time, maybe one minute or one hour. Generally, the life cycle starts from the time when the variable is used, and ends with the time when the variable is used. The time in this period is called the life cycle of the variable. This is similar to the natural spring, summer, and autumn. Every year, it starts from spring until the end of winter. The process from spring to winter is a cycle, but we are used to calling this cycle a year.

The variables in C language include global variables and local variables. The life cycle of global variables is the same as that of the program. That is to say, global variables run through the whole program. The lifecycle of a local variable is not so long. It appears only when it is used. After it is used, it completes its mission and ends its life. The most common is the variables in the function. These variables are all local variables. They start with the function and disappear after the function is run.

Next we will give an actual example to illustrate the lifecycle of a variable:

Int globle_value = 1; void func () {int local_value = 3; printf ("globle_value = % d \ n", globle_value ); // you can use the global variable printf ("local_value = % d \ n", local_value) in this function; // you can use the local variable} int main () in this function () {func (); printf ("globle_value = % d \ n", globle_value ); // you can use the global variable in the main function // printf ("local_value = % d \ n", local_value); // you cannot use the local variable return 0 in the main function ;}

We save the above Code to a file, compile the file, and then run the compiled program. The program running result is as follows:

Globle_value = 1 // you can use the global variable local_value = 3 in the func function. // you can use the global variable globle_value = 1 in the func function. // you can only use global variables in the main function.

As shown in the preceding example, global variables can be used in both the custom func function and the main function because their lifecycles are the same as those of the program. Local variables can only be used in func functions and cannot be used in the main function. if you remove the comments in the program, the compilation error of "variable undefined" will be generated. The reason is that the local variable ends with the completion of the func function, so the local variable cannot be seen in the main function.

Scope of variables and functions

The scope of a variable indicates the valid scope of use of the variable. The variable can be used within a certain range and cannot be used out of this range. For example, the ID card in our daily life can be used at will in China, but it cannot be used after going abroad. We can only use passports. This is because the ID card is in China. After going abroad, it indicates that it is out of the scope of the ID card. In this case, the ID card cannot be used any more. We also use the above example to describe:

The scope of the global variable globle_value is the entire file, so it can be accessed by the func function and main function in the same file. The scope of the local variable local_value is within the function func. Therefore, it can only be used in func functions, and cannot be used elsewhere, because it has exceeded its scope.

With these preparations, it is much easier to introduce static keywords,The static keyword has two main functions: Life Cycle and scope of a variable or function.

For a variable, static does not affect its lifecycle, but initializes the uninitialized variable to 0. However, it will narrow down the scope of the variable, which has no effect on the local variable, mainly to narrow down the scope of the global variable to a file, for example, the global variable globle_value in the above example, it can be used in other files. If you limit it to the static keyword, it can only be used in the current file.

The following is a practical example. For more information, see:

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

In func, we define common variables and static modified variables without initializing them. During compilation, static variables are initialized to 0 instead of common variables. The following is the running result of the program. See:

temp = 134513922 static_local_value = 0 

From the preceding program running result, we can see that the default value of the static variable is 0, while that of the common variable is uncertain.

For a function, static will not affect its lifecycle, but will narrow its scope. This is the same as static variable limitation, so I will not discuss it much.

The complete code is stored in my resources. You can download and use it.

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

For example, it is said that there are a lot of Chinese people named Zhang Wei, that is, there are many people with the same name. If I am Zhang Wei here, there may be only one viewer called Zhang Wei. After all, there are not many readers who listen to my novels. If I post on the Internet, I say: zhang Wei won the prize. There may be a lot of people named Zhang Wei to receive the prize. How can we distinguish so many Zhang Wei? Just use the static keyword to limit it, haha.

Let's talk about the static keyword example. I want to know what examples will be provided later, and I will try again.

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.