C中静态全局变量和非静态全局变量的区别 [英] Difference between static global variable and non-static global variable in C

查看:52
本文介绍了C中静态全局变量和非静态全局变量的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态全局变量"和静态全局变量"有什么区别C中的非静态全局变量"?

What is the difference between 'static global variable' and 'non-static global variable' in C?

请举例说明它们的不同之处.

Please explain how they are different with some example.

(因为,全局静态变量和简单全局变量在整个程序中都保持活动状态,并且可以在任何块中使用.我很困惑如何区分它们.)有人可以用代码解释一下吗?

(Since, both global static and simple global variable remain active throughout program and can be used in any block. I'm confused how to differentiate them.) Can someone explain this with code?

推荐答案

基本上有四种情况:

  • 在函数外声明,没有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.

这篇关于C中静态全局变量和非静态全局变量的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆