外部静态变量的目的是什么?

时间:2021-07-30 00:39:35

K&R c page 83 says the following:

K&R c第83页说明如下:

The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. External static thus provides a way to hide names like buf and bufp in the getch-ungetch combination, which must be external so they can be shared, yet which should not be visible to users of getch and ungetch.

应用于外部变量或函数的静态声明将该对象的范围限制为正在编译的源文件的其余部分。因此,外部静态提供了一种在getch-ungetch组合中隐藏名称(如buf和bufp)的方法,该组合必须是外部的,因此它们可以共享,但getch和ungetch的用户不应该看到它们。

How could any external variable be visible in another file without an extern modifier on the variable in the new file anyway? Is there some type of added protection for variables with the static storage class?

如果在新文件中的变量上没有extern修饰符,任何外部变量如何在另一个文件中可见?对于具有静态存储类的变量,是否存在某种类型的附加保护?

What is the purpose of using static on an external variable? Any simple examples?

在外部变量上使用static的目的是什么?任何简单的例子?


Edit:

I think I'm confusing people with my question, so I'm going to write it out as code. I'm expanding the idea to include functions as well:

我想我的问题让人困惑,所以我打算把它写成代码。我正在扩展包含功能的想法:

contents of file 1

文件1的内容

void somefunc(void);
int x;
int main()
{
    ....
}
void somefunc(void)
{
    ....
}

file 2

int x;
void somefunc(void);
void somefunc(void)
{
    ....
}

Notice that int x and somefunc() in file 1 are not visible in file 2, and vice versa. That is, unless we include an extern modifier on int x and/or somefunc() in either file, the matching function and variable names from the files will be invisible to one another.

请注意,文件1中的int x和somefunc()在文件2中不可见,反之亦然。也就是说,除非我们在任一文件中的int x和/或somefunc()上包含extern修饰符,否则文件中的匹配函数和变量名将彼此不可见。

Why would we need to put static on one of these variables or functions to prevent the variable or function from being visible in the other file if we already have to knowingly use an extern to make the function or variable visible in the other file?

为什么我们需要在其中一个变量或函数上放置静态以防止变量或函数在另一个文件中可见,如果我们必须故意使用extern来使函数或变量在另一个文件中可见?

The code would need to look like this for contents of file 2 to be visible in file 1:

代码需要看起来像这样,文件2的内容在文件1中可见:

extern void somefunc(void);
extern int x;
int main()
{
    ....
}
void somefunc(void)
{
    ....
}

file 2

int x;
void somefunc(void);
void somefunc(void)
{
    ....
}

3 个解决方案

#1


2  

There is a difference of terminology between K&R2 and the C Standard.

K&R2与C标准之间存在术语差异。

K&R2 uses the wording external variable for a file-scope variable, and uses the wording external static to specify a file-scope variable declared with the static storage class specifier. In the C Standard the word external is usually reserved for linkage and not for lexical scope.

K&R2对文件范围变量使用措辞外部变量,并使用外部静态措辞来指定使用静态存储类说明符声明的文件范围变量。在C标准中,外部词通常保留用于链接而不用于词法范围。

#2


1  

Quoting what you quoted:

引用你引用的内容:

External static thus provides a way to hide names like buf and bufp in the getch-ungetch combination, which must be external so they can be shared, yet which should not be visible to users of getch and ungetch.

因此,外部静态提供了一种在getch-ungetch组合中隐藏名称(如buf和bufp)的方法,该组合必须是外部的,因此它们可以共享,但getch和ungetch的用户不应该看到它们。

It just means that the static variables are not function scoped static variables. They are external to functions but they are static in the file.

它只是意味着静态变量不是函数范围的静态变量。它们是函数外部的,但它们在文件中是静态的。

Making them static in a file makes them visible to getch and ungetch but not to other functions in other files.

在文件中将它们设置为静态使得它们对getch和ungetch可见,但对其他文件中的其他函数则不可见。

Update, in response to edited question

更新,以回应编辑过的问题

You said,

Notice that int x and somefunc() in file 1 are not visible in file 2, and vice versa. That is, unless we include an extern modifier on int x and/or somefunc() in either file, the matching function and variable names from the files will be invisible to one another.

请注意,文件1中的int x和somefunc()在文件2中不可见,反之亦然。也就是说,除非我们在任一文件中的int x和/或somefunc()上包含extern修饰符,否则文件中的匹配函数和变量名将彼此不可见。

That is an erroneous conclusion.

这是一个错误的结论。

The line

int x;

equivalent to:

extern int x;
int x;

The line

void somefunc(void);

is equivalent to:

相当于:

extern void somefunc(void);

If you compile the "file 1" and "file 2" and link the resulting object files to create an executable, you will get linker errors to the effect that int x and void somefunc(void) are multiply defined.

如果编译“文件1”和“文件2”并链接生成的目标文件以创建可执行文件,则会出现链接器错误,即int x和void somefunc(void)是多重定义的。

In order to keep them visible only in the respective files, you will have to make them static in the file scope.

为了使它们仅在相应的文件中可见,您必须在文件范围中使它们静态。

static int x;
static void somefunc(void);

#3


0  

  1. What are the uses of the keyword static?
  2. 关键字static的用途是什么?

This simple question is rarely answered completely. Static has three distinct uses in C:

这个简单的问题很少得到完全回答。 Static在C中有三种不同的用途:

(a) A variable declared static within the body of a function maintains its value between function invocations.

(a)在函数体内声明为static的变量在函数调用之间保持其值。

(b) A variable declared static within a module1, (but outside the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized global.

(b)在模块1中声明为静态的变量(但在函数体外部)可由该模块中的所有函数访问。任何其他模块中的功能都无法访问它。也就是说,它是一个本地化的全局。

(c) Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared.

(c)在模块中声明为静态的函数只能由该模块中的其他函数调用。也就是说,函数的范围被本地化到声明它的模块。

Most candidates get the first part correct. A reasonable number get the second part correct, while a pitiful number understand answer (c).

大多数候选人得到第一部分正确。一个合理的数字得到第二部分是正确的,而一个可怜的数字理解答案(c)。

From A ‘C’ Test: The 0×10 Best Questions for Would-be Embedded Programmers

来自A'C'测试:将成为嵌入式程序员的0×10个最佳问题

#1


2  

There is a difference of terminology between K&R2 and the C Standard.

K&R2与C标准之间存在术语差异。

K&R2 uses the wording external variable for a file-scope variable, and uses the wording external static to specify a file-scope variable declared with the static storage class specifier. In the C Standard the word external is usually reserved for linkage and not for lexical scope.

K&R2对文件范围变量使用措辞外部变量,并使用外部静态措辞来指定使用静态存储类说明符声明的文件范围变量。在C标准中,外部词通常保留用于链接而不用于词法范围。

#2


1  

Quoting what you quoted:

引用你引用的内容:

External static thus provides a way to hide names like buf and bufp in the getch-ungetch combination, which must be external so they can be shared, yet which should not be visible to users of getch and ungetch.

因此,外部静态提供了一种在getch-ungetch组合中隐藏名称(如buf和bufp)的方法,该组合必须是外部的,因此它们可以共享,但getch和ungetch的用户不应该看到它们。

It just means that the static variables are not function scoped static variables. They are external to functions but they are static in the file.

它只是意味着静态变量不是函数范围的静态变量。它们是函数外部的,但它们在文件中是静态的。

Making them static in a file makes them visible to getch and ungetch but not to other functions in other files.

在文件中将它们设置为静态使得它们对getch和ungetch可见,但对其他文件中的其他函数则不可见。

Update, in response to edited question

更新,以回应编辑过的问题

You said,

Notice that int x and somefunc() in file 1 are not visible in file 2, and vice versa. That is, unless we include an extern modifier on int x and/or somefunc() in either file, the matching function and variable names from the files will be invisible to one another.

请注意,文件1中的int x和somefunc()在文件2中不可见,反之亦然。也就是说,除非我们在任一文件中的int x和/或somefunc()上包含extern修饰符,否则文件中的匹配函数和变量名将彼此不可见。

That is an erroneous conclusion.

这是一个错误的结论。

The line

int x;

equivalent to:

extern int x;
int x;

The line

void somefunc(void);

is equivalent to:

相当于:

extern void somefunc(void);

If you compile the "file 1" and "file 2" and link the resulting object files to create an executable, you will get linker errors to the effect that int x and void somefunc(void) are multiply defined.

如果编译“文件1”和“文件2”并链接生成的目标文件以创建可执行文件,则会出现链接器错误,即int x和void somefunc(void)是多重定义的。

In order to keep them visible only in the respective files, you will have to make them static in the file scope.

为了使它们仅在相应的文件中可见,您必须在文件范围中使它们静态。

static int x;
static void somefunc(void);

#3


0  

  1. What are the uses of the keyword static?
  2. 关键字static的用途是什么?

This simple question is rarely answered completely. Static has three distinct uses in C:

这个简单的问题很少得到完全回答。 Static在C中有三种不同的用途:

(a) A variable declared static within the body of a function maintains its value between function invocations.

(a)在函数体内声明为static的变量在函数调用之间保持其值。

(b) A variable declared static within a module1, (but outside the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized global.

(b)在模块1中声明为静态的变量(但在函数体外部)可由该模块中的所有函数访问。任何其他模块中的功能都无法访问它。也就是说,它是一个本地化的全局。

(c) Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared.

(c)在模块中声明为静态的函数只能由该模块中的其他函数调用。也就是说,函数的范围被本地化到声明它的模块。

Most candidates get the first part correct. A reasonable number get the second part correct, while a pitiful number understand answer (c).

大多数候选人得到第一部分正确。一个合理的数字得到第二部分是正确的,而一个可怜的数字理解答案(c)。

From A ‘C’ Test: The 0×10 Best Questions for Would-be Embedded Programmers

来自A'C'测试:将成为嵌入式程序员的0×10个最佳问题