One, extern and functionIf a program is made up of multiple source files, the successful compilation generates multiple target files that cannot be run separately because these target files may be associated, such as: The A.obj file may call a function defined in C.obj. Link these related target files together to generate an executable file
1. External functions and intrinsic functions
1) external function: If the function defined in the current file allows access, invocation in other files, it is called an "external function". The C language stipulates that external functions with the same name are not allowed. 2) intrinsic function: If the function defined in the current file can only be used internally, it is called an "intrinsic function". C language to the top different source files can have the same name of the internal functions, do not interfere with each other.
2. External functionsbecause all functions default to external functions, the extern keyword, like auto, is obsolete.
Common practice:
① Create a one.c file and define some functions inside it.
② creates a one.h file that declares the functions defined in one.c.
③ in the Main.c file, include the one.h header file to use the function inside.
3. Examples of external function use
One.c
#include <stdio.h>void one () {printf ("This is method one () \ n");} void () {printf ("This is method," \ n ");} void Three () {printf ("This is method three () \ n");}
one.h
#ifndef __c__one_h#define __c_one_hvoid One (), void One (), void Three (), #endif
first.c
#include <stdio.h> #include "one.h" int main () {one (); three (); return 0;}
Execution Instructions:gcc-c first.c one.c
gcc first.o one.o-o first.exe
First.exe
Execution Result:
Second, static and function 1. Define intrinsic functions
As seen from the above example, the functions defined in ONE.C can be accessed by other source files. In fact, sometimes we want to define an "intrinsic" function that cannot be accessed by other source files. Just use the static keyword modifier function. remark: 1) The above one function with the static keyword decoration, found in the link when the error. 2) During the compile phase: The compiler only detects whether the syntax of a single source file is unreasonable and does not There is no definition of the function, it is only detected when the link is not present, that is, there is no definition. 3) The so-called compile: is to individually check the syntax of each source file is reasonable, do not check the correlation between each source file, a source file is compiled successfully will generate a target file. The so-called Link: is to check the relationship of the target file, the associated target files are grouped together to generate an executable file.
2. Declaring and defining intrinsic functions
#include <stdio.h>static void Test (), int main (int argc, const char * argv[]) { test (); return 0;} static void Test () { printf ("called the Test Function");
Iii. Summary of static, extern and function 1.staticwhen defining a function, it is decorated with the static keyword. An intrinsic function is also known as a "static function". This function can only be used in the files it defines, and if there are external functions of the same name in different files, they do not interfere with each other.
2.externfunctions that are decorated with the extern keyword are external functions that can be omitted. The function defined by default is an external function.
if you want to invoke an external function in another file in a file, you will need to declare the function in the current file with extern, then you can use it, and extern can omit it.
Iv. Static/extern and Variables 1. Location of global variablesThe location of global variables in Java is not required. However, in the C language, the position of the global variable definition is limited. The default one function cannot access the global variables defined after it.
Workaround:
method One: Define the global variable to the front.
method Two: Declare with extern in front.
2. Define the same variable repeatedlyin the C language, you can define a variable repeatedly, which always represents the same variable.
#include <stdio.h>int a=10;int Main () {extern int A; Refers to the global variable, in fact, many times, this line is redundant a=12; Reassign all variables to printf ("%d\n", a); return 0;} int A; Successful execution
#include <stdio.h>int a=10;//global variable int main () {int a =12;//local variable, and all variables a have no relation between printf ("%d\n", a); return 0;} int A;
3. Variable with the same name in different source filesThe same variable is defined in 2 source files, and they represent the same variable.
one.c
#include <stdio.h>int A; void One () {printf ("%d\n", a);}
first.c
#include <stdio.h>int A; void one (); int main () {a =12;one (); return 0;}
4.extern keywordsA variable must have been declared before it can be referenced using the extern keyword.
For example: int A is defined in one.c, the variable is referenced in main.c, extern int a
However, both sides are not allowed to use extern adornments, because none is defined and how to refer to them.
5.staticAll variables of private: variables decorated with the static keyword. The variable can only be valid within the source file.
6. Summary
1) extern can be used to introduce a global variable. 2) By default, a variable can be shared by multiple source files , that is, 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 global variable is limited to the source file that defines the variable . Does not interfere with the same name variables in other source files.
C language (v) extern and Static keywords