Scope of static variables

What do static variables do

Static variables in C can be called static local variables. Generally, the scope and lifetime of a variable defined inside a function is limited to that function. But if you add static in front of it, his life cycle will become larger. That is to say, after leaving the function, the static variable will not be recycled, it will always exist and keep the value of the last time, which is similar to the nature of global variables. However, unlike global variables, the scope of static local variables is still limited to the function. I hope this helps!

Scope of variables defined by Static in C

What is the purpose of the keyword static?

-;In a function body, a variable declared as static maintains its value unchanged while this function is called.

-;Inside a module (but outside the function body), a variable declared static can be accessed by functions used inside the module, but not by other functions outside the module. It is a local global variable.

-;Within a module, a function declared static can only be called by other functions within this module. That is, the function is restricted to use within the local scope of the module in which it is declared.

Examples can be found in my URL below

The role of static keyword

The role of static keyword is as follows:

1. Modifying variables

Static global variable: global variable is modified by adding static in front of it, and the variable becomes a static global variable. As we know, all variables can be accessed in the whole project, but after adding static keyword, this variable can only be accessed within this file. So, here, the role of static is to limit the scope.

Static Local Variables: A local variable becomes a static local variable when the static modifier is added to it. We know that local variables are destroyed when they leave the function in which they are defined, but with the static modifier, they are scoped to the end of the program. So, the role of static here is to limit the life cycle.

2, modifier function

modifier function, the function becomes static function, the function’s scope is limited to this file, and can not be called by other files.

Introduction to the c language

C is a process-oriented, abstract, general-purpose programming language that is widely used in low-level development.C can be compiled and processed in a simple way with low-level memory.C is a high-efficiency programming language that produces only a small amount of machine language and does not require any runtime support to run.