Home  >  Article  >  What is the use of static variables in C language

What is the use of static variables in C language

清浅
清浅Original
2019-05-05 16:42:0420314browse

The role of static variables in C language is to keep the value unchanged during the function call, because static variables have a memory function; but their scope is limited and can only be accessed by functions within the module, etc. .

What is the use of static variables in C language

Recommended: "c Tutorial"

The role of static variables in C language is: static variables It has a memory function and keeps the value unchanged during the function call; the scope is limited and can only be accessed by functions within the module, etc.

What is the use of static variables in C language

In C language, the keyword static means static and has three obvious functions:

1. In the function body, static variables It has a memory effect, that is, the value of a variable declared as static remains unchanged during the process of this function being called.

2. Within a module (but outside a function), its scope is limited. If a variable is declared static, then the variable can be accessed by all functions in the module, but It cannot be accessed by other functions outside the module.

3. Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, they should be described in a header file. The source files that use these functions must include this header file. .

Characteristics of static data members:

1. For non-static data members, each class has its own copy. Static data members are treated as members of the class. No matter how many objects of this class are defined, there is only one copy of the static data members in the program, which is shared and accessed by all objects of this type.

2. Static data members are stored in the global data area. Space must be allocated when defining, so it cannot be defined in the class declaration. Since static data members are shared by all objects of this class, they do not belong to a special class object. When no class object is generated, its scope is visible, that is, programmers can also use it when no instance of the class is generated.

3. Static data members also comply with public, protect, and private access rules.

4. The initialization of static member variables is outside the class, and the static keyword cannot be used at this time. Although the static members of private and protect can be initialized outside the class, they cannot be accessed outside the class.

Advantages of static data members:

1. Static data members do not enter the global name space of the program, so there is no possibility of conflict with other global names in the program. .

2. Information hiding can be achieved. Static data members can be private members, but global variables cannot.

#include
#include 
#include "a.h"
using namespace std;
void fun(int i)
{    static int value = i++;
    cout<

Running result

0
0
0

The above is the detailed content of What is the use of static variables 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
Previous article:What is workflow?Next article:What is workflow?