Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use static keyword in C language

Shulou Source: shulou.com Published: 2022-06-02 08:12:41 10月05日 Update

I believe many inexperienced people don't know what to do about how to use the static keyword in C language. therefore, this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

The C language keyword static has three functions: declare static functions, declare static global variables, and declare static local variables.

Declare a static function

Adding a static in front of the return type of the function is a static function. A static function can only be used in the file in which it is declared, and it is not visible to other files. Different files can use static functions with the same name and do not affect each other. Non-static functions can be referenced directly in another file without even having to use the extern declaration

Declare static global variables

The global variable is defined outside the function body and the compiler initializes it automatically. Ordinary global variables are visible to the entire Kernel, while static global variables are visible only to the current file and other files are not accessible. Therefore, if the defined global variables (or functions) do not need to be shared with other files, it is recommended that you add static to reduce the coupling between programs.

Declare static local variables

A local variable is a variable defined within a function and its scope is much smaller than that of a global variable. The compiler generally does not initialize a normal local variable, which means that its value is initially uncertain unless it is explicitly assigned. Using static's static local variable, the compiler initializes it to 0 even if no initial value is assigned at the time of declaration. And the storage location of the static local variable is also different, even if the function returns, its value will remain the same.

Then compare their addresses with the system memory layout.

The following conclusions are drawn:

1 static functions are not visible in Shell, while global functions are visible in Shell

No matter which kind of function, it is stored in the text segment of the system.

3 global variables with initial values (whether static variables or not) and static local variables with initial values are stored in the data segment of the system

4 Global variables without initial values (whether static variables or not) and static local variables without initial values are stored in the BSS section of the system and are initialized to 0

5 Local variables (non-static, with or without initial values) are stored in the task stack. If no initial value is assigned, 0xeeeeeeee is used

After reading the above, have you mastered how to use the static keyword in C language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Variables static functions global local initial values files storage systems keywords keywords languages compilers compilers different ordinary functions contents that is methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei OPPO Reno MySQL Shulou Technology