In the previous article, it was declared through a header file, and the call had a particularly large vulnerability:
Why the compiler can be chained, because the default is extern-decorated, such as the global scope of the function so that it can be called
Continue to strengthen learning:
This time there are two pairs of C files:
First.c first.h
SECOND.C second.h
FIRST.C Code:
// Multi-File testing " head.h " <stdio.h>void printstr () { printf ("Hello world!\n ");}
First.h Code:
void printstr (); // function definition fields: extending from the declaration point to the end of the source program text
The corresponding, SECOND.C code:
" second.h " <stdio.h>void main () {printstr ();}
Second.h Code:(emphasis)
extern void printstr (); // function definition fields: extending from the declaration point to the end of the source program text
Analysis:
Undoubtedly, in the above Second.h and first.h, we need to use the extern flag to modify the declaration of the PRINTSTR function, so that the PRINTSTR function can be exported to the connection program, which is implemented regardless of the first.c file in the call, Or in the SECOND.C file, the connector will be very smart to follow our wishes to connect him to the definition of the PRINTSTR function in the first.c file, without having to write the same printstr function in the second.c file.
However, the problem follows:
So how do we differentiate between which declarations in the header file are defined in their corresponding. c files, and which ones are not? This may not be necessary, because the Smart Connection program will not be necessary to help us find and export to the connector, regardless of which file is defined, but I think he does. Because we need to know what the specific content of this function is, what function it is, I may want to modify it after I have a new requirement, I need to find the definition of this function in a short time, then let me introduce a man-made specification in C language:
The function declared in the. h file, if defined in its corresponding. c file, does not use the extern modifier when declaring the function, and if it does not, it must be displayed using the extern modifier.
So, in the C-language. h file, we see two types of function declarations. With extern, not with extern, simple and straightforward, one is to refer to an external function, a function of its own life and definition.
Therefore, the extern modifier is not used in first.h (because the corresponding. c file has the definition of the child function Printstr)
Using the extern modifier in second.h
C language extern learning 2 analysis