且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C中静态全局变量和非静态全局变量的区别

更新时间:2022-03-26 05:37:33

基本上有四种情况:

  • 在函数外声明,没有static
  • 在函数外声明,使用static
  • 在函数内部声明,没有static
  • 在函数内部声明,使用static

让我们依次介绍这些.

在函数外声明,没有static

这是一个常规的全局符号.您可以从任何源文件访问它(尽管在其他源文件中,您通常需要一个 extern 声明).

This is a conventional global symbol. You can access it from any source file (although in that other source file, you typically need an extern declaration).

在函数外声明,使用static

这是静态"你问的是全球.您只能在定义它的源文件中访问它.它是私人的"到该源文件,但您可以从该源文件中的任何函数(嗯,实际上,该源文件中出现在其声明下方的任何函数)访问它.与任何全局变量一样,它在程序的生命周期内保持其值.

This is the "static" global you were asking about. You can access it only within the source file where it is defined. It's "private" to that source file, but you can access it from any function in that source file (well, actually, any function in that source file that occurs below its declaration). Like any global variable, it maintains its value for the life of the program.

在函数内部声明,没有static

这是一个传统的局部变量.您只能在该功能内访问它.每次调用该函数时(包括递归调用),您都会获得该变量的一个新实例.如果你不初始化它,它开始包含一个不可预测的值.它不会在调用之间保持其值.

This is a conventional local variable. You can access it only within that function. Each time the function is called (including recursively), you get a new instance of the variable. If you don't initialize it, it starts out containing an unpredictable value. It does not maintain its value between calls.

在函数内部声明,使用static

这是一个静态局部变量.您只能在该功能内访问它.它只有一个副本,在对该函数的所有调用(包括递归调用)之间共享.如果你不初始化它,它从零开始.它在调用之间保持其值.

This is a static local variable. You can access it only within that function. There is exactly one copy of it, shared between all calls to the function (including recursive calls). If you don't initialize it, it starts out zero. It maintains its value between calls.

在其中三种情况下,如果您不提供显式初始化程序,则保证将变量初始化为 0.但在真正的局部变量的情况下,如果您不提供显式初始化程序,它开始包含一个不可预测的值,您不能依赖它.

In three of these cases, if you don't provide an explicit initializer, a variable is guaranteed to be initialized to 0. But in the case of true local variables, if you don't provide an explicit initializer, it starts out containing an unpredictable value, which you can't depend on.

正式地,这里有两个概念,可见性生命周期.真正的全局变量在程序的任何地方都是可见的.静态全局变量仅在其源文件中可见.局部变量仅在其函数中可见.所有全局变量和所有静态变量都有静态持续时间——它们的持续时间与程序一样长.(这些也是保证初始化为 0 的变量.)真正的局部变量具有自动"属性.持续时间——随着包含函数的调用和返回,它们来来去去.

Formally, there are two concepts here, visibility and lifetime. True global variables are visible anywhere in the program. Static global variables are visible only in their source file. Local variables are visible only in their function. All global variables, and all static variables, have static duration — they last for as long as the program does. (Also these are the ones that are guaranteed to be initialized to 0.) True local variables have "automatic" duration — they come and go as the containing function is called and returns.

与持续时间密切相关的是变量实际存储位置的问题.静态持续时间变量通常存储在数据段中.自动持续时间变量通常(尽管不一定)存储在堆栈中.

Closely related to duration is the question of where variables will actually be stored. Static-duration variables are typically stored in the data segment. Automatic-duration variables are typically — though not necessarily — stored on the stack.