Study Notes-20-static and extern keywords 2-functions of Variables

Source: Internet
Author: User

The previous section introduced the functions of static and extern on functions. Static is used to define an internal function and does not allow access to other files. extern is used to define and declare an external function and allow access to other files. Static and extern can also be used for variables, but they are slightly different from functions.

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. Another fact is that if the global variable int A is also included in another source file, all the global variables int A in both 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

 

 

 

 

 

Study Notes-20-static and extern keywords 2-functions of Variables

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.