C language, static variable static basics and usage examples

       The static keyword has several uses. Here is a brief overview about static variables:

1. Static local variables:
   - Static variables defined inside a function.
   - Life cycle: from the start of the program execution to the end of the program.
   - Scope: limited to the function in which it is defined.
   - Static local variables are not reinitialized each time the function is called. It retains the last value.

   void increment() {
       static int count = 0;
       count++;
       printf("%d\n", count);
   }

          The value of count is incremented each time increment() is called.

          The following is the use of the static function. A function x is defined in the newly defined function fun, which is modified by static.

void fun();
int x = 3;
int main() {
    int i;
    for(i = 1; i < x; i++) fun();
}
void fun() {
    static int x=1;
    x*=x+1;
    printf("%d", x);
}

        A static local variable x is defined inside the function fun. Although it has the same name as the global variable x, they occupy different memory units. For the static variable x, because its lifetime is the same as that of the program, its value after each call It will be reserved for a long time, and its initial value is assigned once when the program is running, and the initial value will not be assigned after calling. So the first call outputs 2, and the second call outputs 6 (2*3).

2. Static global variables:
   - Static variables defined outside the function but inside the file.
   - Life cycle: from the start of the program execution to the end of the program.
   - Scope: Only in the file where it is defined.
   - Not visible to functions outside the file.
   static int globalVar = 10;
3. Static class members (mainly in object-oriented languages ​​such as C++, Java):
   - belong to a class rather than a specific object instance.
   - All object instances share a copy of the same static member.
   - need to use class name instead of object to access static members.

   class MyClass {
   public:
       static int staticVar;
   };

   int MyClass::staticVar = 0;

           In Java it's something like:

   public class MyClass {
       public static int staticVar = 0;
   }

4. Static functions:
   - In C and C++, there can be static functions.
   - Visible only in the file in which it is defined.
   - Cannot access non-static members of a class (in C++).

   static void myFunction() {
       // ...
   }

5. Static methods (Object Oriented Languages):
   - Belong to a class not an object instance.
   - Cannot access non-static members of the class.
   - Typically used to implement functionality that does not require access or modification of object-specific data.

6. Other uses: The static keyword has other meanings and uses in certain programming languages ​​and contexts.

When using the static keyword, you need to be clear about the effect you want and the syntax rules of the programming language you are using.

Guess you like

Origin blog.csdn.net/qq_52119661/article/details/132376199