Does the static keyword affect scope?

时间:2016-06-10 16:16:57

标签: c++ c scope static global

In C89, does the static keyword affect scope?

My software lead told me:

"A variable marked static at the top of a file doesn't technically have global scope any longer. Static is a scope qualifier as well as a storage keyword. Scope is a concept that covers visibility of symbols, though visibility is automatically compiled to have storage duration intrinsically tied in by almost all languages. By this I mean that you can't name a scope that doesn't also define the storage duration in C/C++. Expression scope is not user defined and in C/C++ covered by l-param and r-param Block scope is fully lexical in C/C++ by user defined bodies Function scope is fully lexical in C/C++ by user defined bodies and declarations File scope does not technically exist in C/C++, as globals and module scope take over depending upon lexicon Module scope is keyword defined using static in C/C++, other scope lexicon change the rules for access but the visibility remains module based Global scope is the default in C/C++ when no other scope applies and is lexically controlled by the extern keyword The issue is that static is not JUST a scope qualifier as a keyword. It is a scope qualifier AND a memory keyword."

I'm confused. I've always thought that static relates to the visibility between translation units and the storage duration of the variable. Both of which are unrelated to scope. Is this not the case? Is the static/scope relationship different in C++?

4 个答案:

答案 0 :(得分:6)

  

文件顶部标记为static的变量在技术上不再具有全局范围。

“全局范围”不是C中存在的概念。正确的术语是文件范围。在C ++中,存在一个类似的概念,称为全局命名空间。加班人似乎把这两个词合并在一起。

  

Static是范围限定符以及storage关键字。

static不是范围限定符,它是存储类说明符。 static会影响链接和存储持续时间,但不会影响范围。

  

范围是一个涵盖符号可见性的概念,尽管可见性会自动编译为存储持续时间内在   几乎所有语言。

范围与符号的可见性无关(在链接器意义上)。联系确实(因此它被称为链接年龄)。第二个条款是胡言乱语。

  

我的意思是你不能命名一个也没有用C / C ++定义存储持续时间的范围。

这句话也没有意义。考虑块范围内的局部静态变量。它具有静态存储持续时间,即使块作用域定义了自动存储变量。

  

表达式范围不是用户定义的,在l-param和r-param涵盖的C / C ++中

“表达范围”毫无意义。 “l-param”和“r-param也是毫无意义的词语。

跳过关于“词汇”和“模块”的部分,因为它没有任何意义。

  

问题是static不仅仅是作为关键字的范围限定符。它是范围限定符和内存关键字。

同样,static与范围或内存无关。使用这个过于简化的解释基本上省略了存储持续时间,范围和初始化的所有其他方面,因此它只是简单的不起作用。

答案 1 :(得分:3)

C11标准的第6.2.1节定义了什么"范围"的意思是:

  

对于标识符指定的每个不同实体,标识符仅在称为其范围的程序文本的区域内可见(即,可以使用)。由相同标识符指定的不同实体具有不同的范围,或者在不同的名称空间中。范围有四种:函数,文件,块和函数原型。 (函数原型是声明其参数类型的函数的声明。)

C89 / 90规范的第3.1.2.1节几乎相同:

  

标识符仅在区域内可见(即,可以使用)   程序文本称为其范围。有四种范围:   函数,文件,块和函数原型。 (功能原型   是声明其类型的函数的声明   参数。)

所以没有这样的东西 全局范围,至少就C标准而言。在任何函数或块之外定义的标识符具有 file 范围,static的存在与否对此没有影响,仅对符号的链接< / em>,这是完全不同的东西(但你的领导可能会与术语&#34;范围&#34;混淆或混淆。)

答案 2 :(得分:1)

你的线人很困惑。 static对范围没有任何影响。

文件范围用词不当,因为您可以使用#include指令或其他假设的依赖于实现的工具构建多文件转换单元。全局范围也是用词不当,因为程序可以由多个翻译单元组成。模块仍然不是语言的一部分。

static会影响链接,但这与范围不同。

答案 3 :(得分:0)

关键字static在C中有多种用途。例如,在顶部的C源文件中,您可能有:

#include <stdio.h>
//  .. other includes and comments and stuff

int  globallyVisibleInt = 0;    // a variable that other compilation units can see
static int fileVisibleInt = 0;  // a variable visible in the file from this point

变量fileVisibleInt在应用程序加载时创建并初始化,但是如果您尝试从其他编译单元访问它,则在尝试链接时会从链接器中收到错误。

您还可以在函数中使用static来创建将存在并保持状态的变量。

int myFunc (int k)
{
    static int mySavedInt = 0;   // create and initialize a permanent int variable

    if (k > 10) {
        mySavedInt = k;  // use the variable to save a value for the next time function is called
    } else if (mySavedInt > 22) {
        // do some stuff
    }
}

static变量对文件中其他来源可见的点是它出现在源文件中的点,它的可见性由各种范围规则控制。例如,函数中定义的static仅在该函数中可见,或者static用于其他一些范围更小的范围,例如iffor那么它是一个只在该范围内可见的永久变量。

有各种各样的短语是常用的,但不一定是技术上准确的,或者你会在标准中找到的东西。例如,短语&#34;全球范围&#34;对我来说意味着事物在编译单元之外是可见的。通过&#34;模块&#34;我会假设功能。对于大多数日常活动,语言的松散效果都很好。

在C ++中,static可能会有所不同,具体取决于您使用的是{+ 1}}等C ++结构,还是使用class作为方法和成员的限定符,或者如果您是使用旧的C方式。

另见:

File Scope and Global Scope: C & C++

Why file scope static variables have to be zero-initialized?

Dr. Dobbs: Scope Regions in C++