- Second, static and function
- Iii. Summary of static, extern and function
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 types of variables are described in the previous section, and different types of variables have different storage types, different life cycles, and different scopes. This is about 2 more important keywords: static and extern.
Static and extern can be used not only on variables, but also on functions. This first describes their effect on the function.
One, extern and function
In the third and fourth speaking, I mentioned one sentence: if there are multiple source files (. c) in a program, a successful compilation produces a corresponding number of target files (. obj) that cannot be run alone, because these target files may be associated with each other. For example, a.obj might call a function defined in C.obj. These associated target files are linked together to generate the executable file.
First to understand the 2 concepts:
- External functions: If the function defined in the current file allows other files to be accessed, called, is called an external function. The C language stipulates that external functions with the same name are not allowed.
- intrinsic function: If the function defined in the current file does not allow other files to be accessed, invoked, or used internally, it is called an intrinsic function. The C language specifies that different source files can have intrinsic functions with the same name and do not interfere with each other.
The next step is to show a function that calls another source file definition in a source file, such as calling the one function defined in one.c in MAIN.C.
1. First defines the one function in one.c
If you want this one function to be accessed by MAIN.C, then the one function must be an external function. The complete definition is to add the extern keyword.
However, this extern is as obsolete as the Auto keyword, which can be omitted, because by default all functions are external functions. We can simplify:
2. Next, I want to call the one function in one.c in the main function of MAIN.C
How can I invoke the one function in one.c? You may have 2 ideas:
Idea 1: Write one () directly in the main function;
This is certainly not possible, because the main function does not know the existence of one function, how to invoke it? This will be an error in the standard C compiler, but it is only a warning in Xcode.
Idea 2: Include the one.c file in the MAIN.C
We all know that the role of # include is purely a copy of the content, so the equivalent
Well, it looks like it's right. Oh, before the main function, you define one function, and then call the one function in the main function. It's syntactically right, so compiling is no problem. But this program is not likely to run successfully because of the error when linking. We have defined the one function in the one.c, and now in the MAIN.C to define the one function, C language rules do not allow the same name of the external function, the linker will find in One.obj and main.obj defined the same function, will directly error, Xcode error message is this:
Duplicate symbol _one is that one of the identifiers repeats, and linker refers to the linker.
The above 2 ideas are not feasible, in fact, the idea is consistent: let main function know the existence of one function. The correct approach should be to pre-declare the one function in front of the main function (see Clearly, it is a declaration, not a definition, the definition and the declaration are different).
3. Advance declaration of the one function in front of the main function
You want to take the external functions defined in the other source files and declare that the complete practice should be to use the extern keyword to denote "external functions" that refer to others
Running the program, from the console output, you can find that the "one function defined in ONE.C" has been successfully called by the "Main.c main function".
Someone might come up with an idea: what if there are other source files that define this one function, except one.c? What is the one function that the main function calls? Rest assured, there is absolutely no such situation, not just said, not allowed to repeat the definition of the same external function, or the linker will error, so there will only be an external one function.
This is what the extern keyword does to a function: it is used to define and declare an external function. In fact, extern and auto as waste, can be omitted completely. As a result, we can simplify this:
In order to develop in a modular way, in a formal project, we will write the declaration of one function to another header file, of course, the name of the header file is best meaningful and normative, such as called One.h. Later, who wants to call this one function, including one.h this header file is OK. So the final code structure is this:
Second, static and function
1. Defining intrinsic functions
As can be seen from the above example, the one function defined in one.c can be accessed by other source files. In fact, sometimes we might want to define an "intrinsic function", which means that you don't want other files to access the functions defined in this file. This is very simple, you just need to add a static keyword when defining a function.
(We will modify the code based on the example above)
I added a static to the front of void One (), which represents an intrinsic function.
Then you will find that the program does not work, in the link when the error. The reason for the error is simple: We have called the one function defined in one.c in Main.c, but now one.c is an "intrinsic" function that does not allow access to other files. Let's take a look at the error message:
The 1th red box in the undefined symbols ... This means that the one identifier is not defined, that is, one is not found, and the 2nd Red Box linker indicates that the linker has an error.
But this program can be compiled successfully, because we declare the one function in front of the main function (the function declaration and definition is different), this function declaration can be understood as: in the syntax, cheat the main function, tell it that one function exists, So, from a grammatical point of view, the main function can call one function. Does the one function exist or is it defined? The compiler is no matter. In the compile phase, the compiler will only detect a single source file syntax is not reasonable, do not detect the function is not defined, only when the link to detect that the function does not exist, that is, there is no definition.
Let's discuss one more question, why is it possible to compile successfully in many cases, but when the link is wrong? As long as you understand the role of compiling and linking is good.
The so-called compilation, is to check each source file syntax is reasonable, and does not check the correlation between each source file, a source file compiled successfully generated a target file.
The so-called link, is to check the relationship of the target file, the associated target file is combined to generate the executable file.
After reading these 2 concepts, and then go back to think about the error of the previous report, it should be fully understood.
2. Declaring intrinsic functions
We can also declare an intrinsic function with static
1 #include <stdio.h> 2 3 static void Test (), 4 5 int main (int argc, const char * argv[]) 6 {7 test (); 8 return 0; 9}10 one static void Test () { printf ("called the Test Function"); 13}
A test function is defined in line 11th, an intrinsic function, followed by a declaration of the test function in line 3rd, and then the test () function can be called in line 7th.
Iii. Summary of static, extern and function
1.static
* When defining a function, add static at the leftmost side of the function to declare the function as an intrinsic function (also called a static function) so that the function can only be used in the file where its definition resides. If there are intrinsic functions of the same name in different files, they do not interfere with each other.
* Static can also be used to declare an intrinsic function
2.extern
* When defining a function, if you add the keyword extern to the leftmost side of the function, it means that the function is an external function that can be called by other files. The C language specifies that if you omit extern when defining a function, it is implied as an external function.
* In a file to invoke external functions in other files, you need to declare the external function in the current file with extern, and then you can use, where the extern can also be omitted.
C language 19-static and extern keyword 1-effect on functions