Home  >  Article  >  Backend Development  >  What does static mean in c language?

What does static mean in c language?

angryTom
angryTomOriginal
2020-02-06 13:47:0182966browse

What does static mean in c language?

What does static mean in c language

In c language, static is used to describe static variables.

1. If it is defined outside the function, its effect is similar to that of a global variable, that is, the static variable can be used in the current C program file.

2. If it is defined inside a function, then this variable is only initialized once. Even if the function is called again, the static variable will not be initialized again, so the value of this variable will always be saved. Then, when we call the function again, the results saved during the last function call are still saved.

Recommended learning: c language video tutorial

In C language, the function of static keyword is as follows:

1 . When modifying variables, the static local variables modified by static are only executed once, and the life cycle of the local variables is extended until the program is finished running.

2. When static modifies a global variable, this global variable can only be accessed in this file and cannot be accessed in other files, even if it is extern external declaration.

3. If static modifies a function, this function can only be called in this file and cannot be called by other files. Local variables modified by Static are stored in the static variable area of ​​the global data area.

Example:

static int a;
int b;
void func(void)
{
    static int c;
    int d;
}

In the above program, a and b are both global variables. The difference between them is that b can be used by other files, and a can only Can be used in this file. This is the effect of static on global variables.
The difference between c and d is that d is an automatic variable. After the func function is executed, d will be automatically released. But c will not be released. The next time the func function is called, the value of c will retain the last value and continue to be used.

The above is the detailed content of What does static mean in c language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn