[Dark horse programmer] ---- static and extern keywords 2-role to variables

Source: Internet
Author: User

 

Both C and Java have the concept of global variables, but their usage is still somewhat different.

1. in Java, there is no strict location rule for the definition of global variables.

Global variables can be defined at the beginning of the class or at the end of the class, that is, a method can access the variables defined after it.

As you can see, the test method defined in row 4th can access the variable a defined in row 8th, which is completely correct.

2. In C, the location defined by global variables is limited.

By default, a function cannot access the global variables defined after it.

In the main function defined in row 4th, the compiler directly reports an error when trying to access variable a defined in row 9th.

There are two ways to solve this problem:

1st Methods: Define variable A before the main function

In this way, the compiler will not bother you.

 

2nd Methods: declare variable A in advance before the main function

That is to say, let the main function know the existence of variable A. The main function does not need to care about the location where variable A is defined.

* The complete variable Declaration requires the extern keyword.

The first line declares variable A, and the second line defines variable A. Again, the declaration and definition are two different things. In the first row, the variable a defined in the second row is operated.

Note: you cannot omit the definition of row 10th. Only the declaration of row 3rd is left, because extern is used to declare a variable that has already been defined.

 

3. Repeatedly define the same variable

* In fact, you can also define a again before the main function.

You may be surprised to see this scene, but the compiler will not report an error. In this case, variable A of row 3rd and row 10th represents the same variable.

 

* Similarly, if we write the global variable int A countless times, all of them represent the same variable.

The variables A in rows 3rd to 6th and 13th to 17th represent the same variable.

 

* You can declare global variable A as a local variable before using it !!!

Note: rows 2nd, 5th, 6th, and 10th all represent the same variable. In fact, we can see from the color (light blue) of Line 1 A that this A is still a global variable.

(This is a feature of xcode. If a global variable is accessed within the function, the global variable is light blue. If a local variable is accessed inside the function, the local variable is normal black. Of course, different development tools have different display solutions)

 

* However, if you remove the extern of the 5th rows, the situation is totally different. I believe you know what the situation is like if you have programming experience.

Rows 2nd and 10th represent the same global variable, while rows 5th and 6th represent a local variable. There is no half-cent relationship with the global variable outside. In fact, the color (black) of rows 1 and 6 can be seen as a local variable.

4. variables with the same name in different source files

As mentioned above, no matter how many times you write the global variable int A in a source file, they represent the same variable. There is also the fact that if the global variable int A; exists in another source file, all the global variables int A; of the two source files represent the same variable.

Note: global variables A in Main. C and test. C both represent the same variable.

We can prove that:

First, define a function in test. C to view the value of.

Modify the value of a to 10 in row 9th of Main. C, and call the test function of test. C to check the value of A in test. C.

The output in the console proves everything.

 

* Of course, the extern keyword is still applicable, for example:

And

Or:

And

In the above two cases, the global variables A used in test. C and Main. C still represent the same variable.

Note that all the variables A in the two files cannot use extern. The following method is incorrect:

And

Because extern is used to declare a defined variable, both files are declaring the variable, no one defines the variable, and an error is certainly returned during the link:

The general error indicates that identifier A is not defined.

5. Static keywords

However, in many cases, we do not want to share the global variables in the source file with other source files, which is equivalent to private global variables, so you must use the static keyword to define the variables.

In this way, variables A of test. C and Main. C represent different variables, which are unrelated and independent from each other. That is to say, Main. C cannot access variable A in test. C. Therefore, after modifying a to 10 in Main. C, A in test. C is still 0. Output result :.

In fact, static can also be used to modify local variables. As mentioned in variable type, I will not elaborate on it anymore.

 

* Because main. C has no permission to access variable A in test. C, the following statement is incorrect:

And

Extern is used to declare variables that have been defined and can be accessed, although test. C has defined variable A, but test. in C, the scope of variable A is limited to test. c file, Main. C has no access permission, so main. the extern in C is obsolete.

When the link is connected, an error is reported: identifier A is not defined

Unless main. c defines a variable A by itself, so extern is valid. However, variable A in Main. C and test. C respectively represents different variables.

Vi. Summary of static and extern

1. extern can be used to declare a global variable, but cannot be used to define a variable.

2. By default, a global variable can be shared by multiple source files. In other words, global variables with the same name in multiple source files represent the same variable.

3. if you add the static keyword when defining a global variable, the static keyword is used to restrict the scope of the global variable. It can only be used in files that define the global variable, variables with the same name in other source files do not interfere with each other

4. The role of static on variables:
Define an internal variable

The role of extern on variables:
Declare an external variable

Static functions:
Define and declare an internal function

The role of extern on functions:
Define and declare an external function (which can be omitted)

5.

1 # include <stdio. h> 2 3/* 4 Use Cases for static modification of local variables: 5 1. if the call frequency of a function is particularly high, 6 2. A variable value in this function is fixed 7 */8 9 void test () 10 {11 static double Pi = 3.14; 12 13 double ZC = 2 * pI * 10; 14 15 int A = 0; 16 A ++; 17 printf ("the value of A is % d \ n", ); // 118 19/* 20 static modification of local variables: 21 1> prolong the lifecycle of local variables: when the program ends, the local variable will be destroyed 22 2> the local variable scope is not changed 23 3> all test functions share a variable B24 */25 static int B = 0; 26 B ++; 27 printf ("the value of B is % d \ n", B); // 328} 29 30 int main () 31 {32 for (INT I = 0; I <100; I ++) {33 test (); 34} 35 36 37 test (); 38 39 test (); 40 41 test (); 42 43 44 return 0; 45}

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.