C语言 C中的静态和外部有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3684450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:25:27  来源:igfitidea点击:

What is the difference between static and extern in C?

c

提问by Sambhav jain

What is the difference between staticand externin C?

C 中的static和 有什么区别extern

采纳答案by Danail

From http://wiki.answers.com/Q/What_is_the_difference_between_static_and_extern:

来自http://wiki.answers.com/Q/What_is_the_difference_between_static_and_extern

The staticstorage class is used to declare an identifier that is a local variable either to a function or a file and that exists and retains its value after control passes from where it was declared. This storage class has a duration that is permanent. A variable declared of this class retains its value from one call of the function to the next. The scope is local. A variable is known only by the function it is declared within or if declared globally in a file, it is known or seen only by the functions within that file. This storage class guarantees that declaration of the variable also initializes the variable to zero or all bits off.

The externstorage class is used to declare a global variable that will be known to the functions in a file and capable of being known to all functions in a program. This storage class has a duration that is permanent. Any variable of this class retains its value until changed by another assignment. The scope is global. A variable can be known or seen by all functions within a program.

静态存储类用于声明的标识符是一个局部变量或者函数或一个文件,并存在和控制从它被宣布,其中通过后保持其值。此存储类具有永久的持续时间。此类声明的变量从函数的一次调用到下一次调用都保留其值。范围是本地的。变量只能由它在其中声明的函数知道,或者如果在文件中全局声明,则只能由该文件中的函数知道或看到。这个存储类保证变量的声明也将变量初始化为零或所有位关闭。

所述的extern存储类用于声明将是已知的功能在一个文件中并能够被已知的所有功能在一个程序中的全局变量。此存储类具有永久的持续时间。此类的任何变量都保留其值,直到被另一个赋值更改。范围是全球性的。程序中的所有函数都可以知道或看到变量。

回答by gablin

staticmeans a variable will be globally known only in this file. externmeans a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files.

static意味着变量将仅在此文件中全局已知。extern意味着在另一个文件中定义的全局变量在这个文件中也是已知的,并且也用于访问在其他文件中定义的函数。

A local variable defined in a function can also be declared as static. This causes the same behaviour as if it was defined as a global variable, but is only visible inside the function. This means you get a local variable whose storage is permanent and thus retain its value between calls to that function.

函数中定义的局部变量也可以声明为static. 这会导致与定义为全局变量相同的行为,但仅在函数内部可见。这意味着您将获得一个局部变量,其存储是永久的,因此在调用该函数之间保留其值。

I'm no C expert so I might be wrong about this, but that's how I've understood staticand extern. Hopefully someone more knowledgable will be able to provide you with a better answer.

我不是 C 专家,所以我可能是错的,但这就是我所理解的,static并且extern. 希望有更懂行的人能够为您提供更好的答案。

EDIT:Corrected answer according to comment provided by JeremyP.

编辑:根据 JeremyP 提供的评论更正了答案。

回答by Jonathan Leffler

You can apply staticto both variables and functions. There are two answers that discuss the behaviour of staticand externwith respect to variables, but neither really covers functions. This is an attempt to rectify that deficiency.

您可以同时应用于static变量和函数。有两个答案讨论了变量的行为staticextern关于变量的行为,但都没有真正涵盖函数。这是为了弥补这一缺陷的一种尝试。

TL;DR

TL; 博士

  • Use static functions whenever possible.
  • Only declare external functions in headers.
  • Use the headers where the functions are defined and where the functions are used.
  • Don't declare functions inside other functions.
  • Don't exploit the GCC extension with function definitions nested inside other functions.
  • 尽可能使用静态函数。
  • 只在头文件中声明外部函数。
  • 使用定义函数和使用函数的头文件。
  • 不要在其他函数中声明函数。
  • 不要利用嵌套在其他函数中的函数定义来利用 GCC 扩展。

External functions

外部功能

By default, functions in C are visible outside the translation unit (TU — basically the C source file and included headers) in which they are defined. Such functions can be called by name from any code that notifies the compiler that the function exists — usually by a declaration in a header.

默认情况下,C 中的函数在定义它们的翻译单元(TU——基本上是 C 源文件和包含的头文件)之外是可见的。此类函数可以从任何通知编译器该函数存在的代码中按名称调用——通常通过头文件中的声明。

For example, the header <stdio.h>makes visible declarations of functions such as printf(), fprintf(), scanf(), fscanf(), fopen(), fclose(), and so on. If a source file includes the header, it can call the functions. When the program is linked, the correct library must be specified to satisfy the function definition. Fortunately, the C compiler automatically provides the library that provides (most of) the functions in the standard C library (and it usually provides a lot more functions than just those). The 'most of' caveat applies because on many systems (Linux, for instance, but not macOS), if you use functions declared in the <math.h>header, you need to link with the maths library ('math' library if you're American), which usually is indicated by the option -lmon the linker command line.

例如,标头<stdio.h>使诸如printf()fprintf()scanf()fscanf()fopen()fclose()、 等函数的声明可见。如果源文件包含标头,则它可以调用函数。链接程序时,必须指定正确的库以满足函数定义。幸运的是,C 编译器会自动提供提供标准 C 库中(大部分)函数的库(并且它通常提供的函数远不止这些)。“大多数”警告适用,因为在许多系统(例如 Linux,但不是 macOS)上,如果您使用在<math.h>头文件中声明的函数,则需要与 maths 库(如果您是美国人,则为“math”库)链接,通常由选项表示-lm在链接器命令行上。

Note that external functions should be declared in headers. Each external function should be declared in one header, but one header may declare many functions. The header should be used both in the TU where each function is defined and in each TU that uses the function. You should never need to write a declaration for a global function in a source file (as opposed to a header file) — there should be a header to declare the function and you should use that header to declare it.

请注意,应在标头中声明外部函数。每个外部函数都应该在一个头文件中声明,但一个头文件可以声明多个函数。在定义每个功能的 TU 和使用该功能的每个 TU 中都应使用该标头。您永远不需要在源文件(而不是头文件)中为全局函数编写声明——应该有一个头文件来声明该函数,并且您应该使用该头文件来声明它。

Static functions

静态函数

As an alternative to generally visible functions, you can make your own functions static. This means that the function cannot be called by name from outside the TU in which it is defined. It is a hidden function.

作为一般可见函数的替代方案,您可以创建自己的函数static。这意味着不能从定义它的 TU 外部按名称调用该函数。这是一个隐藏的功能。

The primary advantage of static functions is hiding details which the outside world doesn't need to know about. It is a basic but powerful information hiding technique. You also know that if a function is static, you do not need to look for uses of the function outside the current TU, which can greatly simplify the search. However, if the functions are static, there can be multiple TUs which each contain a definition of a function with the same name — each TU has its own function, which may or may not do the same thing as a function with the same name in a different TU.

静态函数的主要优点是隐藏外界不需要知道的细节。这是一种基本但功能强大的信息隐藏技术。你也知道如果一个函数是静态的,你不需要在当前 TU 之外寻找该函数的用途,这可以大大简化搜索。但是,如果功能是static,可以有多个课时,每个包含一个函数的名称相同的定义-每个TU都有自己的功能,这可能会或可能不会做同样的事情,作为一个函数在一个相同的名字不一样的恩。

In my code, I qualify all functions except main()with the keyword staticby default — unless there's a header that declares the function. If I subsequently need to use the function from elsewhere, it can be added to the appropriate header and the keyword staticremoved from its definition.

在我的代码中,除了默认main()使用关键字之外,我对所有函数进行了限定static——除非有一个声明该函数的标头。如果我随后需要从其他地方使用该函数,则可以将其添加到适当的标头中,并将关键字static从其定义中删除。

Declaring functions inside other functions

在其他函数中声明函数

It is possible, but very inadvisable, to declare a function inside the scope of another function. Such declarations fly in the face of Agile Development maxims such as SPOT (Single Point of Truth) and DRY (Don't Repeat Yourself). They're also a maintenance liability.

在另一个函数的作用域内声明一个函数是可能的,但非常不可取。此类声明与诸如 SPOT(单点真理)和 DRY(不要重复自己)等敏捷开发格言背道而驰。它们也是维护责任。

However, you can, if you so desire, write code such as:

但是,如果您愿意,您可以编写如下代码:

extern int processor(int x);

int processor(int x)
{
    extern int subprocess(int);
    int sum = 0;
    for (int i = 0; i < x; i++)
        sum += subprocess((x + 3) % 7);
    return sum;
}

extern int subprocess(int y);

int subprocess(int y)
{
    return (y * 13) % 37;
}

The declaration in processor()suffices for it to use subprocess(), but is otherwise unsatisfactory. The externdeclaration before the definition is necessary if you use GCC compiler options such as:

声明processor()足以使用subprocess(),但在其他方面并不令人满意。的extern,如果你使用GCC编译器选项,如定义之前的声明是必要的:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
>     -c process.c
process.c:12:5: error: no previous prototype for ‘subprocess' [-Werror=missing-prototypes]
 int subprocess(int y)
     ^~~~~~~~~~
cc1: all warnings being treated as errors
$

This is, I find, a good discipline, similar to what C++ enforces. It's another reason I make most functions static, and define the functions before they're used. The alternative is to declare static functions at the top of the file and then define them in whatever order seems appropriate. There are some merits to both techniques; I prefer to avoid the need to declare and define the same function in the file by defining before use.

我发现这是一个很好的纪律,类似于 C++ 强制执行的规则。这是我将大多数函数设为静态并在使用之前定义函数的另一个原因。另一种方法是在文件顶部声明静态函数,然后以任何合适的顺序定义它们。这两种技术都有一些优点。我更喜欢通过在使用前定义来避免在文件中声明和定义相同的函数。

Note that you cannot declare a staticfunction within another function, and if you attempt to define a function such as subprocess()as a static function, the compiler gives an error:

请注意,您不能static在另一个函数中声明一个函数,并且如果您尝试定义一个函数,例如subprocess()静态函数,编译器会给出一个错误:

process.c:12:16: error: static declaration of ‘subprocess' follows non-static declaration
     static int subprocess(int y)
                ^~~~~~~~~~
process.c:5:20: note: previous declaration of ‘subprocess' was here
         extern int subprocess(int);
                    ^~~~~~~~~~

Since functions that are externally visible should be declared in a header, there is no need to declare them inside a function, so you should never run into this as a problem.

由于外部可见的函数应该在头文件中声明,因此无需在函数内部声明它们,因此您永远不应该遇到这个问题。

Again, the externis not necessary in the function declaration inside the function; if omitted, it is assumed. This can lead to unexpected behaviour in novice programs here on SO — you sometimes find a function declaration where a call was intended.

同样,extern在函数内部的函数声明中不需要 ; 如果省略,则假定。这可能会导致 SO 上的新手程序出现意外行为 - 您有时会在预期调用的地方找到函数声明。

With GCC, the option -Wnested-externsidentifies nested externdeclarations.

对于 GCC,该选项-Wnested-externs标识嵌套extern声明。

Called by name vs called by pointer

名称调用 vs 指针调用

If you have a nervous disposition, stop reading now. This gets hairy!

如果你有紧张的性格,现在停止阅读。这变得毛茸茸的!

The 'called by name' comment means that if you have a declaration such as:

'call by name' 注释意味着如果您有如下声明:

extern int function(void);

you can write in your code:

你可以在你的代码中写:

int i = function();

and the compiler and linker will sort things out so that the function is called and the result used. The externin the declaration of the function is optional but explicit. I normally use it in a header file to match the declaration of those rare global variables — where the externis not optional but mandatory. Many people disagree with me on this; do as you wish (or must).

并且编译器和链接器将整理出来,以便调用函数并使用结果。将extern在函数的声明是可选的,但明确的。我通常在头文件中使用它来匹配那些罕见的全局变量的声明——其中extern不是可选的而是强制性的。在这一点上,很多人不同意我的看法;随心所欲(或必须)。

Now what about static functions? Suppose the TU reveal.cdefines a function static void hidden_function(int) { …?}. Then, in another TU openness.c, you cannot write :

那么静态函数呢?假设 TUreveal.c定义了一个函数static void hidden_function(int) { …?}。然后,在另一个 TU 中openness.c,您不能写:

hidden_function(i);

Only the TU that defines the hidden function can use it directly. However, if there's a function in reveal.cthat returns a function pointer to the hidden_function(), then the code openness.ccan call that other function (by name) to get a pointer to the hidden function.

只有定义隐藏函数的 TU 才能直接使用它。但是,如果其中有一个函数reveal.c返回指向 的函数指针hidden_function(),则代码openness.c可以调用该其他函数(按名称)以获取指向隐藏函数的指针。

reveal1.h

reveal1.h

extern void (*(revealer(void)))(int);

Obviously, that's a function that takes no arguments and returns a pointer to a function that takes an intargument and returns no value. No; it isn't pretty. One of the times it makes sense to use typedefon pointers is with pointers to functions (reveal2.h):

显然,这是一个不带参数的函数,并返回一个指向带int参数但不返回值的函数的指针。不; 它不漂亮。typedef在指针上使用有意义的时候之一是使用指向函数的指针 ( reveal2.h):

typedef void (*HiddenFunctionType)(int);
extern HiddenFunctionType revealer(void);

There: much simpler to understand.

那里:更容易理解。

See Is it a good idea to typedef pointersfor a general discussion on the subject of typedefand pointers; the short summary is "it isn't a good idea except perhaps with function pointers".

有关和指针主题的一般性讨论,请参阅键入定义指针是否是个好主意typedef;简短的总结是“这不是一个好主意,除非使用函数指针”。

reveal1.c

reveal1.c

#include <stdio.h>
#include "reveal1.h"

static void hidden_function(int x)
{
    printf("%s:%s(): %d\n", __FILE__, __func__, x);
}

extern void (*(revealer(void)))(int)
{
    return hidden_function;
}

Yes, it is legitimate (but very unusual) to define the function with an explicit extern— I very, very seldom do it, but here it emphasizes the role of externand contrasts it with static. The hidden_function()can be returned by revealer(), and could be called by code inside reveal.c. You can remove the externwithout changing the meaning of the program.

是的,这是合法的(但极不寻常的)来定义有明确的功能extern-我非常,非常很少做到这一点,但在这里它强调的作用extern,并对比它static。该hidden_function()可以通过返回revealer(),并且可以通过代码中调用reveal.c。您可以在extern不改变程序含义的情况下删除。

openness1.c

openness1.c

#include <stdio.h>
#include "reveal1.h"

int main(void)
{
    void (*revelation)(int) = revealer();
    printf("%s:%s: %d\n", __FILE__, __func__, __LINE__);
    (*revelation)(37);
    return 0;
}

This file cannot usefully contain a direct call by name to hidden_function()because it is hidden in the other TU. However, the revealer()function declared in reveal.hcan be called by name and it returns a pointer to the hidden function, which can then be used.

该文件不能有效地包含按名称的直接调用,hidden_function()因为它隐藏在另一个 TU 中。但是,revealer()inreveal.h中声明的函数可以通过名称调用,并且它返回一个指向隐藏函数的指针,然后可以使用该指针。

reveal2.c

reveal2.c

#include <stdio.h>
#include "reveal2.h"

static void hidden_function(int x)
{
    printf("%s:%s(): %d\n", __FILE__, __func__, x);
}

extern HiddenFunctionType revealer(void)
{
    return hidden_function;
}

openness2.c

openness2.c

#include <stdio.h>
#include "reveal2.h"

int main(void)
{
    HiddenFunctionType revelation = revealer();
    printf("%s:%s: %d\n", __FILE__, __func__, __LINE__);
    (*revelation)(37);
    return 0;
}

Sample outputs

样本输出

Not the most exciting output in the world!

不是世界上最令人兴奋的输出!

$ openness1
openness1.c:main: 7
reveal1.c:hidden_function(): 37
$ openness2
openness2.c:main: 7
reveal2.c:hidden_function(): 37
$

回答by Rajat

Both of these modifiers have something to do with memory allocation and linking of your code. The C standard[3] refers to them as storage-class specifiers. Using those allows you to specify when to allocate memory for your object and/or how to link it with the rest of the code. Let's have look on what exactly is there to specify first.

这两个修饰符都与内存分配和代码链接有关。C 标准 [3] 将它们称为存储类说明符。使用这些允许您指定何时为您的对象分配内存和/或如何将其与其余代码链接。让我们先看看到底有什么需要指定。

Linking in C

在 C 中链接

There are three types of linkage – external, internal and none. Each declared object in your program (i.e. variable or function) has some kind of linkage – usually specified by the circumstances of the declaration. Linkage of an object says how is the object propagated through the whole program. Linkage can be modified by both keywords extern and static .

存在三种类型的链接——外部链接、内部链接和无链接。程序中每个声明的对象(即变量或函数)都有某种链接——通常由声明的环境指定。对象的链接说明对象如何在整个程序中传播。可以通过关键字 extern 和 static 修改链接。

External Linkage

外部联动

Objects with external linkage can be seen (and accessed) through the whole program across the modules. Anything you declare at file (or global) scope has external linkage by default. All global variables and all functions have external linkage by default.

可以通过模块的整个程序查看(和访问)具有外部链接的对象。默认情况下,您在文件(或全局)范围内声明的任何内容都具有外部链接。默认情况下,所有全局变量和所有函数都具有外部链接。

Internal Linkage

内部联动

Variables and functions with internal linkage are accessible only from one compilation unit – the one they were defined in. Objects with internal linkage are private to a single module.

具有内部链接的变量和函数只能从一个编译单元访问——它们被定义在那个编译单元中。具有内部链接的对象对单个模块是私有的。

None Linkage

无 联动

None linkage makes the objects completely private to the scope they were defined in. As the name suggests, no linking is done. This applies to all local variables and function parameters, that are only accessible from within the function body, nowhere else.

无链接使对象在定义它们的范围内完全私有。顾名思义,不进行链接。这适用于所有局部变量和函数参数,它们只能从函数体内访问,不能从其他任何地方访问。

Storage duration

储存期限

Another area affected by these keywords is storage duration, i.e. the lifetime of the object through the program run time. There are two types of storage duration in C – static and automatic.

受这些关键字影响的另一个方面是存储持续时间,即通过程序运行时间的对象的生命周期。C 中有两种类型的存储持续时间——静态和自动。

Objects with static storage duration are initialized on program startup and remain available through the whole runtime. All objects with external and internal linkage have also static storage duration. Automatic storage duration is default for objects with no linkage. These objects are allocated upon entry to the block in which they were defined and removed when the execution of the block is ended. Storage duration can be modified by the keyword static .

具有静态存储持续时间的对象在程序启动时初始化,并在整个运行时保持可用。所有具有外部和内部链接的对象也具有静态存储持续时间。没有链接的对象默认为自动存储持续时间。这些对象在进入定义它们的块时被分配,并在块的执行结束时被移除。可以通过关键字 static 修改存储持续时间。

Static

静止的

There are two different uses of this keyword in the C language. In the first case, static modifies linkage of a variable or function. The ANSI standard states:

这个关键字在 C 语言中有两种不同的用法。在第一种情况下,静态修改变量或函数的链接。ANSI 标准规定:

If the declaration of an identifier for an object or a function has file scope and contains the storage-class specifier static , the identifier has internal linkage.

如果对象或函数的标识符声明具有文件范围并包含存储类说明符 static ,则该标识符具有内部链接。

This means if you use the static keyword on a file level (i.e. not in a function), it will change the object's linkage to internal, making it private only for the file or more precisely, compilation unit.

这意味着如果您在文件级别(即不在函数中)使用 static 关键字,它将更改对象与内部的链接,使其仅对文件或更准确地说是编译单元私有。

/* This is file scope */

int one; /* External linkage. */
static int two; /* Internal linkage. */

/* External linkage. */
int f_one()
{
    return one;
}

/* Internal linkage. */
static void f_two()
{
    two = 2;
}

int main(void)
{
    int three = 0; /* No linkage. */

    one = 1;
    f_two();

    three = f_one() + two;

    return 0;
}

The variable and function() will have internal linkage and won't be visible from any other module.

变量和 function() 将具有内部链接,并且不会从任何其他模块可见。

The other use of static keyword in C is to specify storage duration. The keyword can be used to change automatic storage duration to static. A static variable inside a function is allocated only once (at program startup) and therefore it keeps its value between invocations

C 中 static 关键字的另一个用途是指定存储持续时间。该关键字可用于将自动存储持续时间更改为静态。函数内的静态变量仅分配一次(在程序启动时),因此它在调用之间保持其值

#include <stdio.h>

void foo()
{
    int a = 10;
    static int sa = 10;

    a += 5;
    sa += 5;

    printf("a = %d, sa = %d\n", a, sa);
}

int main()
{
    int i;

    for (i = 0; i < 10; ++i)
        foo();
}

The output will look like this:

输出将如下所示:

a = 15, sa = 15
a = 15, sa = 20
a = 15, sa = 25
a = 15, sa = 30
a = 15, sa = 35
a = 15, sa = 40
a = 15, sa = 45
a = 15, sa = 50
a = 15, sa = 55
a = 15, sa = 60

Extern

外部

The extern keyword denotes, that “this identifier is declared here, but is defined elsewhere”. In other words, you tell the compiler that some variable will be available, but its memory is allocated somewhere else. The thing is, where? Let's have a look at the difference between declaration and definition of some object first. By declaring a variable, you say what type the variable is and what name it goes by later in your program. For instance you can do the following:

extern 关键字表示“此标识符在此处声明,但在别处定义”。换句话说,您告诉编译器某个变量将可用,但它的内存分配在其他地方。问题是,在哪里?让我们先看看一些对象的声明和定义之间的区别。通过声明一个变量,您可以说明该变量的类型以及稍后在程序中使用的名称。例如,您可以执行以下操作:

extern int i; /* Declaration. */
extern int i; /* Another declaration. */

The variable virtually doesn't exist until you define it (i.e. allocate memory for it). The definition of a variable looks like this:

该变量实际上不存在,直到您定义它(即为其分配内存)。变量的定义如下所示:

int i = 0; /* Definition. */

You can put as many declaration as you want into your program, but only one definition within one scope. Here is an example that comes from the C standard:

你可以在你的程序中加入任意多的声明,但在一个范围内只能有一个定义。这是来自 C 标准的示例:

/*  definition, external linkage */
int i1 = 1;
/*  definition, internal linkage */
static int i2 = 2;
/*  tentative definition, external linkage */
int i3;

/*  valid tentative definition, refers to previous */
int i1;
/*  valid tenative definition, refers to previous */
static int i2;
/*  valid tentative definition, refers to previous */
int i3 = 3;

/* refers to previous, whose linkage is external */
extern int i1;
/* refers to previous, whose linkage is internal */
extern int i2;
/* refers to previous, whose linkage is external */
extern int i4;

int main(void) { return 0; }

This will compile without errors.

这将编译没有错误。

Summary

概括

Remember that static – the storage-class specifier and static storage duration are two different things. Storage duration is a attribute of objects that in some cases can be modified by static , but the keyword has multiple uses.

请记住,静态——存储类说明符和静态存储持续时间是两个不同的东西。Storage duration 是对象的一个​​属性,在某些情况下可以通过 static 修改,但关键字有多种用途。

Also the extern keyword and external linkage represent two different areas of interest. External linkage is an object attribute saying that it can be accessed from anywhere in the program. The keyword on the other hand denotes, that the object declared is not defined here, but someplace else.

此外,extern 关键字和外部链接代表了两个不同的领域。外部链接是一个对象属性,表示可以从程序的任何地方访问它。另一方面,关键字表示声明的对象不是在这里定义的,而是在其他地方定义的。

回答by Vikas Vaibhav

StaticThe static variables declared with the keyword static. The static variable initial value is 0. The static variables has block file scope scope.

静态用关键字static 声明的静态变量。静态变量初始值为 0。静态变量具有块文件作用域。

ExternA program in C, particularly when it is large, can be broken up into smaller programs. After compiling these, each program file can be joined together to form the large program. These small programs modules that combine together may need some variable that is used by all of them. In C, such a provision can be made by specifying these variables, accessible to all the small program modules, as an external storage class variable. These variables are global to all the small program modules that are formed as separate files. The keyword for declaring such global variables is extern.

ExternC 中的程序,特别是当它很大时,可以分解成更小的程序。编译这些之后,每个程序文件就可以组合在一起形成大程序。这些组合在一起的小程序模块可能需要一些变量供它们所有人使用。在 C 中,可以通过将所有小程序模块都可以访问的这些变量指定为外部存储类变量来做出这样的规定。这些变量对于作为单独文件形成的所有小程序模块是全局的。声明此类全局变量的关键字是 extern。

Such a global variable is declared like any other variable in one of the program modules while the declaration of these variables is preceded with the keyword extern in all other combining program modules.

这种全局变量的声明与程序模块之一中的任何其他变量一样,而这些变量的声明在所有其他组合程序模块中以关键字 extern 开头。

The program modules may also be a function or a block. These variables remain in existence as long as the program is in execution and their existence does not terminate upon the exit of a function or block or a program module from its state of execution. These variables are stored in the primary memory and their default value is zero. Storage classes in C

程序模块也可以是功能或块。只要程序正在执行,这些变量就会一直存在,并且它们的存在不会在函数或块或程序模块从其执行状态退出时终止。这些变量存储在主存储器中,它们的默认值为零。 C 中的存储类