Jun 01, 2021 Article blog
Several types of C-language automatic variables are mentioned above, and here we say the types of static variables that are linked outside the c-language.
Static variables with external links have file scopes, external links, and static storage periods. T his category is sometimes referred to as the external storage class, and the variables that belong to that category are called external variables. By placing the defining declaration of a variable outside all functions, an external variable is created.
Of course, in order to indicate that the function uses an external variable, you can declare it again in the function with the keyword
extern
I
f an external variable used by one source code file is defined in another source code file, the variable must be declared in that file with
extern
Here's what it looks like:
int Errupt; /* externally defined variable */
double Up[100]; /* externally defined array */
extern char Coal; /* mandatory declaration if */
/* Coal defined in another file */
void next(void);
int main(void)
{
extern int Errupt; /* optional declaration */
extern double Up[]; /* optional declaration */
...
}
void next(void)
{
...
}
Note that when declaring an
Up
array in
main()
(which is an optional claim), you do not need to indicate the array size because the first declaration already provides array size information. T
wo
extern
declarations in
main()
can be omitted because external variables have file scopes, so
Errupt
and
Up
are visible from the declaration to the end of the file. T
hey appear there, just to illustrate that
main()
function uses both variables. I
f you omit the
extern
keyword in the function, it is equivalent to creating an automatic variable.
Remove
extern
from the following statement:
extern int Errupt;
becomes:
int Errupt;
This causes the compiler to create an automatic variable called
Errupt
in
main()
I
t is a separate local variable, unlike the original external variable
Errupt
T
he local variable is only visible in
main()
but the external variable
Errupt
is also visible to other functions of the file, such as next(). I
n short, when a statement in a block is executed, variables in the block scope "hide" variables of the same name in the file scope. I
f you have to use a local variable with the same name as an external variable, you can express this intent explicitly using
auto
store category descriptor in the declaration of the local variable. E
xternal variables have static storage periods. T
herefore, the array
Up
and its values persist regardless of whether the program executes to
main()
next()
or other functions. T
he following three examples demonstrate some usage of external and automatic variables. T
here is an external variable in Example
Hocus
The variable is visible to both
main()
and
magic()
/* Example 1 */
int Hocus;
int magic();
int main(void)
{
extern int Hocus; // Hocus declared external
...
}
int magic()
{
extern int Hocus; // same Hocus as above
...
}
Example 2 has an external variable,
Hocus
that is visible to both functions.
This time,
magic()
is visible by default.
/* Example 2 */
int Hocus;
int magic();
int main(void)
{
extern int Hocus; // Hocus declared external
...
}
int magic()
{
// Hocus not declared but is known
...
}
In Example 3, four separate variables are created.
Hocus
variable in
main()
is automatic by default and is private to
main()
Hocus
variable in
magic()
is explicitly declared automatic and only
magic()
is available. N
either the external variable
Hocus
is visible to
main()
nor
magic()
but is visible to other functions that do not create local
Hocus
variables in the file.
Finally,
Pocus
is an external variable, magic is
magic()可见
but
main()
is not because
Pocus
is declared after
main()
/* Example 3 */
int Hocus;
int magic();
int main(void)
{
int Hocus; // Hocus declared, is auto by default
...
}
int Pocus;
int magic()
{
auto int Hocus; // local Hocus declared automatic
...
}
These three examples demonstrate that the scope of an external variable is from the declaration to the end of the file. I
n addition, the life of the external variable is described.
External variables
Hocus
and
Pocus
persist in program operation because they are not limited to any function and do not disappear when a function returns.
External variables are similar to automatic variables and can also be explicitly initialized. U nlike automatic variables, if external variables are not initialized, they are automatically initialized to 0. T his principle also applies to externally defined array elements. Unlike automatic variables, file scope variables can only be initialized using constant expressions:
int x = 10; // ok, 10 is constant
int y = 3 + 20; // ok, a constant expression
size_t z = sizeof(int); // ok, a constant expression
int x2 = 2 * x; // not ok, x is a variable
(As long as it is not a longer array,
sizeof
expression can be considered a constant expression.)
)
Let's look at an example of using an external variable. S
uppose you have two
main()
and
critic()
both of which access the variable
units
You can declare units on top of these two
units
as shown in Program Listing 12.4 (Note: The purpose of this example is to demonstrate how an external variable works, not its typical usage).
/* global.c -- uses an external variable */
#include <stdio.h>
int units = 0; /* an external variable */
void critic(void);
int main(void)
{
extern int units; /* an optional redeclaration */
printf("How many pounds to a firkin of butter?n");
scanf("%d", &units);
while ( units != 56)
critic();
printf("You must have looked it up!n");
return 0;
}
void critic(void)
{
/* optional redeclaration omitted */
printf("No luck, my friend. Try again.n");
scanf("%d", &units);
}
Here's an example of the program's output:
How many pounds to a firkin of butter?
14
No luck, my friend. Try again.
56
You must have looked it up!
(We did.)
Note how
critic()
reads the second value of
units
W
hen
while
loop ends,
main()
also knows the new value
units
S
o
main()
function and
critic()
can access the same variable through the identifier
units
In
C
terms, units have file
units
external links, and static storage periods.
Defining
units
outside (i.e. outside) of all function
units
is an external variable that is visible to all functions below
units
definition.
Therefore,
critics()
can use
units
variable directly.
Similarly,
main()
can also access
units
directly.
However, there is indeed the following statement in
main()
extern int units;
In this case, the above declaration is intended primarily to indicate that the function uses this external variable. T
he storage category descriptor
extern
tells the compiler that anywhere in the function that uses
units
to the same variable defined outside the function.
Again,
main()
and
critic()
use externally defined
units
Both
C99
and
C11
standards require the compiler to recognize the first 63 characters of a local identifier and the first 31 characters of an external identifier. T
his revises the previous standard of the compiler identifying the first 31 characters of a local identifier and the first six characters of an external identifier. T
he compiler you are using may also execute the previous rules.
The external variable name is stricter than the local variable name because the external variable name also follows the local environment rules and is subject to more restrictions.
The differences between defining and declaring variables are further described below. Consider the following example:
int tern = 1; /* tern defined */
main()
{
external int tern; /* use a tern defined elsewhere */
Here,
tern
is declared twice. T
he first declaration reserves storage space for a variable, which constitutes the definition of a variable. T
he second declaration only tells the compiler to use the
tern
variable that was previously created, so this is not a definition. T
he first declaration is called a defined declaration, and the second declaration is called a reference declaration.
The keyword
extern
indicates that the claim is not a definition because it instructs the compiler to query its definition elsewhere.
Suppose it says this:
extern int tern;
int main(void)
{
The compiler assumes that
tern
is actually defined elsewhere in the program, perhaps in another file. T
his claim does not cause storage space to be allocated.
Therefore, instead of creating an external definition with the keyword
extern
use it only to reference existing external definitions.
An external variable can only be initialized once and must be defined. Suppose you have the following code:
// file one.c
char permis = 'N';
...
// file two.c
extern char permis = 'Y'; /* error */
file_two
declaration in file_two is wrong because the defined declaration in
file_one.c
has created and initialized
permis
The above is a description of the static variable type of the external link to the C language, and students interested in the C language can take a look at the tutorial
C tutorial: https://www.w3cschool.cn/c/