The difference between C language static function and ordinary function

 1. Global variables

    Add the keyword static before the global variable, the global variable is defined as a global static variable, then:

         1) Location in memory: static storage area (static storage area exists during the entire program running)

         2) Initialization: Uninitialized global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary, unless it is displayed initialized)

         3) Scope: Global static variables are not visible outside the file where they are declared. To be precise, start from the definition to the end of the file.

   The benefits of defining global static variables:

        <1> will not be accessed or modified by other files.

        <2> Variables with the same name can be used in other files without conflict.

 

2. Local static variables

Add the keyword static before the local variable, the local variable is defined as a local static variable.

        1) Location in memory: static storage area.

        2) Initialization: Uninitialized global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary, unless it is displayed initialized).

        3) Scope: The scope is still a local scope. When the function or statement block that defines it ends, the scope ends.

Note: When static is used to modify a local variable, it changes the storage location of the local variable from the original stack storage to a static storage area. However, the local static variable is not destroyed after leaving the scope, but still resides in the memory until the end of the program, but we can no longer access it.

When static is used to modify a global variable, it changes the scope of the global variable (it is not visible outside the file where it is declared), but it does not change its storage location, it is still in the static storage area.

 

3. Static functions

Add the keyword static before the return type of the function, and the function is defined as a static function.

The definition and declaration of functions are extern by default, but static functions are only visible in the file where they are declared and cannot be used by other files.

 The benefits of defining static functions:

        <1> Functions with the same name can be defined in other files without conflict.

        <2> Static functions cannot be used by other files. The storage specifiers auto, register, extern, static correspond to two storage periods: automatic storage period and static storage period. auto and register correspond to automatic storage period. A variable with automatic storage period is created when entering the program block where the variable is declared, it exists when the program block is active, and is cancelled when the program block is exited.

The keywords extern and static are used to describe variables and functions with static storage duration. Local variables declared with static have static storage duration (static storage duration), or static extent (static extent). Although his value remains valid between function calls, the visibility of its name is still limited to its local domain. A static local object is initialized for the first time when the program execution reaches the declaration of the object.

 Due to the above characteristics of static variables, some specific functions can be realized, such as counting the number of times, declaring a local variable of the function and setting it to static type as a counter, so that the function can be counted every time it is called. This is the best way to count the number of times a function is called, because this variable is closely related to the function, and the function may be called in many different places, so it is difficult to count from the perspective of the caller.

The benefits of using static functions in C language:

Static functions are automatically allocated in a storage area that has been used until the application instance is exited, which avoids pushing the stack when calling the function, which is much faster.

The keyword "static" is translated into Chinese as "static", so internal functions are also called static functions. But the meaning of "static" here does not refer to the storage method, but refers to the scope of the function is limited to this file. The advantage of using internal functions is that when different people write different functions, you don't have to worry about whether the functions you define will have the same name as the functions in other files, because the same name does not matter.

 

Summary: The difference between static function and ordinary function

Functions modified with static are limited to this source file and cannot be called by code files other than this source file. Ordinary functions are extern by default, that is, they can be called by other code files.

Add the keyword static before the return type of the function, and the function is defined as a static function. The definition and declaration of ordinary functions are extern by default, but static functions are only visible in the file where they are declared and cannot be used by other files.

That's it for today's sharing. Please point out any errors!

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!

[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning books:

 

Programming learning video:

 

 

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112704837