"C language" 20-static and extern keyword 2-action on variables

Source: Internet
Author: User

directory of this document
    • First, in Java, the definition of global variables does not have strict location rules
    • In the C language, the position of the global variable definition is limited.
    • Third, repeat the same variable definition
    • Iv. variable with the same name in different source files
    • Five, the static keyword
    • Vi. Summary of Static and extern

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore

The previous talk describes the function of static and extern on functions, static is used to define an intrinsic function, does not allow other file access; extern is used to define and declare an external function that allows access to other files. Static and extern are also useful for variables, but they are somewhat different from the function.

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

Back to top one, in Java, the definition of a global variable has no strict location rules

A global variable can be defined at the very beginning of a class, or at the end of a class, and a method can access a variable defined after it.

As you can see, the test method defined in line 4th can access the variable a defined in line 8th, which is perfectly fine.

Back to top second, in C, the location of the global variable definition is limited

By default, a function does not have access to the global variables defined after it

In the main function defined in line 4th, attempting to access the variable a defined in line 9th, the compiler immediately gave an error.

There are 2 ways to resolve this error:

The 1th approach: Define variable A before the main function

So the compiler won't bother you.

2nd approach: Advance declaration of variable A before the main function

That is, let the main function know that the existence of the variable A is OK, as to where the variable A is defined, the main function is not in the tube.

* Full variable declaration requires the extern keyword

Line 3rd is declaring variable A, and line 10th is defining variable A, again emphasizing that declarations and definitions are different. The 6th row is the variable a defined by line 10th.

Note: You cannot omit the definition of line 10th, leaving only the declaration of line 3rd, because extern is used to declare a variable that has already been defined.

Go back to the top three, define the same variable repeatedly

* In fact, you can also directly before the main function to define a

You may be surprised to see this, but the compiler won't get an error. In this case, the variable a of line 3rd and line 10th represents the same variable.

* And so on, if we write countless global variables int A; they represent the same variable.

The variable A in lines 3rd through 6th, 13th through 17th represents the same variable.

* Also note that we can also declare the global variable A as a local variable and then use!!!

Note: The 2nd, 5th, 6th, and 10th lines all represent the same variable. In fact, from line 6th A's color (light blue) can be seen, this a is still a global variable.

(This is the feature of Xcode, if the global variable is accessed inside the function, the global variable will show a light blue color, if the function inside the local variable is accessed, the local variable will show normal black.) Of course, different development tools have different display scenarios)

* However, if you remove the extern of line 5th, then the situation is completely different, and I believe you know what it is like to have programming experience.

The 2nd and 10th lines represent the same global variable, and the 5th and 6th lines are a local variable, with no half-dime relationship to the outside global variable. In fact, from the 5th, 6 line a color (black) can be seen as a local variable.

Back to top iv. variables of the same name in different source files

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

Note: The global variable A in MAIN.C and test.c represents the same variable.

We can prove that:

First, define a function in test.c to see the value of a

Then in line 9th of MAIN.C, modify the value of a to 10, and then call TEST.C's test function to see the value of a in test.c.

The output of the console has proven everything.

* Of course, the extern keyword is still applicable, such as:

And

Or is:

And

In both cases, the global variable a used in test.c and MAIN.C still represents the same variable

Note that it is not allowed to use extern for all variable A of two files, the following is wrong:

And

Because extern is used to declare a variable that has already been defined, these two files are declared variables, no one defines the variable, and when the link is correct, the error will be:

The approximate error means: The identifier A is not defined

Back to top five, static keywords

But most of the time, we don't want the global variables in the source file to be shared with other source files, which is equivalent to a private global variable, then you have to use the static keyword to define the variable.

In so doing, the variable a of test.c and main.c represents different variables, which are not connected and do not interfere with each other. That is, MAIN.C cannot access variable A in test.c, so when a is modified 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, this in the "variable type" said, no longer elaborated.

* Because MAIN.C has no access to variable A in test.c, the following notation is incorrect:

And

extern is used to declare a variable that has already been defined and can be accessed, although the variable A is defined in test.c, but the scope of variable A in test.c is limited to test.c file, MAIN.C does not have access, so the extern in Main.c is obsolete.

Error when linking: identifier A is not defined

Unless the MAIN.C itself defines a variable a, the extern is valid, but at this time the variable a in MAIN.C and test.c represents different variables, respectively.

Back to top Vi. summary of static and extern

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

2. By default, a global variable can be shared by multiple source files, which means that a global variable with the same name in multiple source files represents the same variable

3. If you add the static keyword when defining a global variable, the effect of static is to limit the scope of the global variable, to be used only in the file that defines the global variable, and not to interfere with the same name variable in other source files

"C language" 20-static and extern keyword 2-action on 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.