In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use static variables with external links in C language". The editor shows you the operation process through an actual case, which is simple, fast and practical. I hope this article "how to use static variables with external links in C language" can help you solve the problem.
Externally linked static variables have file scope, external links, and static storage periods. This category is sometimes called an external storage category (external storage class), and variables that belong to this category are called external variables (external variable). Placing the definition declaration (defining declaration) of a variable outside all functions creates an external variable.
Of course, to indicate that the function uses external variables, you can declare it again in the function with the keyword extern. If an external variable used by one source code file is defined in another source code file, the variable must be declared in that file using extern. As follows:
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 the Up array in main () (which is an optional declaration), you don't need to specify the size of the array, because the first declaration already provides the array size information. The two extern declarations in main () can be omitted completely, because the external variables have file scope, so Errupt and Up are visible from the declaration to the end of the file. They are there just to show that the main () function uses these two variables. If you omit the extern keyword in the function, you are creating an automatic variable. Remove the extern from the following declaration:
Extern int Errupt
It becomes:
Int Errupt
This causes the compiler to create an automatic variable called Errupt in main (). It is an independent local variable, different from the original external variable Errupt. This local variable is visible only in main (), but the external variable Errupt is also visible to other functions in the file, such as next (). In short, when a statement in a block is executed, a variable in the block scope "hides" a variable with the same name in the file scope. If you have to use a local variable with the same name as an external variable, you can use the auto storage class specifier in the declaration of the local variable to express this intention explicitly. External variables have static storage periods. Therefore, the array Up and its value always exist, regardless of whether the program executes to main (), next (), or other functions. The following three examples demonstrate some use of external and automatic variables. In example 1, there is an external variable Hocus. This 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...}
In example 2, there is an external variable Hocus, which 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. The Hocus variable in main () is an automatic variable by default and is private to main (). The Hocus variable in magic () is explicitly declared automatic, and only magic () is available. The external variable Hocus is not visible to main () and magic (), but is visible to other functions in the file that do not create local Hocus variables. Finally, Pocus is an external variable, and magic () is visible, but main () is not visible 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. In addition, it also illustrates the lifetime of external variables. The external variables Hocus and Pocus always exist in the program because they are not limited to any function and do not disappear after a function returns.
Initialize external variables
External variables, like automatic variables, can also be explicitly initialized. Unlike automatic variables, if external variables are not initialized, they are automatically initialized to 0. This principle also applies to externally defined array elements. Unlike in the case of automatic variables, file scope variables can only be initialized with constant expressions:
Int x = 10; / / ok, 10 is constantint y = 3 + 20; / / ok, a constant expressionsize_t z = sizeof (int); / / ok, a constant expressionint x2 = 2 * x; / / not ok, x is a variable
(sizeof expressions can be treated as constant expressions as long as they are not variable-length arrays. )
Use external variables
Let's look at an example of using external variables. Suppose you have two functions, main () and critic (), that access the variable units. You can declare units on top of these two functions, as shown in listing 12.4 (note that the purpose of this example is to demonstrate how external variables work, not its typical usage).
/ * global.c-- uses an external variable * / # include 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 upsetting"); return 0 } void critic (void) {/ * optional redeclaration omitted * / printf ("No luck, my friend. Try again.n "); scanf (" d ", & units);}
Here is an example of the output of the program:
How many pounds to a firkin of butter?14No luck, my friend. Try again.56You must have looked it up! (We did.)
Notice how critic () reads the second value of units. When the while loop ends, main () also knows the new value of units. So both the main () function and critic () can access the same variable through the identifier units. Described in C terms, units has file scope, external links, and static storage periods.
If you define units outside all function definitions (that is, outside), units is an external variable that is visible to all functions under the units definition. Therefore, critics () can use the units variable directly.
Similarly, main () can also access units directly. However, there is a declaration in main () as follows:
Extern int units
In this case, the above declaration is mainly to indicate that the function is going to use this external variable. The storage category specifier extern tells the compiler that any place in the function that uses units refers to the same variable defined outside the function. Again, both main () and critic () use externally defined units.
External name
Both C99 and C11 standards require compilers to recognize the first 63 characters of local identifiers and the first 31 characters of external identifiers. This modifies the previous standard that the compiler recognizes the first 31 characters of the local identifier and the first 6 characters of the external identifier. The compiler you are using may also enforce the previous rules. The rules of external variable names are stricter than local variable names because external variable names also follow local environment rules and are subject to more restrictions.
Definition and declaration
The following further describes the difference between defining and declaring variables. Consider the following example:
Int tern = 1; / * tern defined * / main () {external int tern; / * use a tern defined elsewhere * /
Here, tern is declared twice. The first declaration reserves storage space for the variable, which constitutes the definition of the variable. The second declaration only tells the compiler to use the previously created tern variable, so this is not a definition. The first declaration is called a defined declaration (defining declaration), and the second declaration is called a referential declaration (referencing declaration). The keyword extern indicates that the declaration is not a definition because it instructs the compiler to query its definition elsewhere.
Suppose it is written like this:
Extern int tern;int main (void) {
The compiler assumes that the actual definition of tern is somewhere else in the program, perhaps in another file. This declaration does not cause storage space to be allocated. Therefore, don't create an external definition with the keyword extern, just use it to reference an existing external definition.
An external variable can only be initialized once and must be done when the variable is defined. Suppose you have the following code:
/ / file one.cchar permis = 'Nabilitan.Universe / file two.cextern char permis =' Yoyo; / * error * /
The declaration in file_two is incorrect because the defined declaration in file_one.c has already created and initialized the permis.
This is the end of the content on "how to use static variables of external links in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.