Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use static keyword in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report