全局变量和静态全局变量 [英] Global variable and static global variable

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

问题描述

我听说所有的全局变量都是静态的..


如果是这样那么2个声明之间有什么区别:

I heard that all global variables are static..

If its so then what is the difference between the 2 declarations:

展开 | 选择 | Wrap | 行号

推荐答案

它们都在程序的整个生命周期内。声明为static的变量只在声明它的文件中具有作用域,因为可以使用extern声明从其他文件访问声明为static而不声明的变量。
They are both in memory for the entire lifetime of the program. The variable that is declared static only has scope in the file in which it is declared where as the variable declared without static can be accessed from other files using an extern declaration.


"静" C中的关键字取决于它是否出现在大括号分隔的块中。请参阅以下代码段。
The meaning of the "static" keyword in C depends on whether or not it appears within a brace-delimited block. Refer to the following code snippet.
展开 | 选择 | Wrap | 行号


C ++中的相同


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

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