- ..clause 1..: Sharing of variable
- ..clause 2..: Extern declaration
- ..clause 3..: Repetition prevention of header file
In the foregoing paragraph, the program was divided into two or more files by a minimum composition.
When you do development separately for two or more source files
Though the necessity for sharing not only the function but also the variable and the constant, etc. is caused
The variable cannot be shared by the method of the preceding chapter.
For instance, when declaring a variable as follows in the header file
The error of the meaning that the declaration overlaps is displayed, and it is not possible to compile.
It is necessary to understand the meaning of the declaration to understand this error more accurately./* sum.h */ int sum(int min,int max); int Public;
When the variable and the function are declared, the compiler memorizes the name and shape.
It is a function that is called that it is this declaration.
And, the compiler actually makes the variable and the function at the same time.
It is a function that is called that it is this definition.
In a current variable, this declaration and the definition were done always at the same time.
Only because declaration teaches compiler shape of variable and function
There is no problem no matter even the shape is the same, and how declared.
However, make the substance of the function and the variable in the definition.
It becomes an error because it becomes indistinguishable if the same function and the variable are made many times.
[ The prototype is declared ].
Though only the prototype declaration is described and it succeeded in the preceding chapter
It defines because of not doing only the prototype declaration declares to this.
Therefore, many prototype declarations (If it is the same writing) can be written.
It explained no declaration because of doing of the variable the declaration and the definition in the preceding clause at the same time two or more times.
It is necessary only to declare two or more times to solve this problem, and to finish defining by one time.
In that case, extern (Exe turn) declaration only to declare is prepared.
The usage of the extern declaration is easy. However, before the current declaration It is only described extern.
[ Extern declaration ]
Extern is used and the doing definition is a declaration method of not doing only as for declare.
/* sum.h */ extern int sum(int min,int max); extern int Public;
The variable can be shared by a different source file by the use of this extern declaration.
First of all, extern declares some variables in the header file.
In this, variable Public : It is possible to share by all source files to which sum.h is done in include./* sum.h */ extern int sum(int min,int max); extern int Public; /* Extern declaration of variable */
In this, variable Public : From main.c It comes to be able to use it from sum.c./* sum.c */ int Public; /* Making of substance of variable */ int sum(int min,int max) { int num; num = (min + max) * (max - min + 1) / 2; return num; }
/* main.c */ #include <stdio.h> # include "sum.h" int main(void) { int value; value = sum(50,100); printf("%d\n",Public); return 0; }
The execution result of this program becomes as follows./* sum.c */ int Public; int sum(int min,int max) { int num; num = (min + max) * (max - min + 1) / 2; Public = 100; return num; }
Contents of the sum function only say that [nanoha] and a correction as it is were troublesome.
100
[ Necessary minimum ]
Do not abuse it so much though sharing the variable is a very convenient technique.
Originally, the purpose of dividing into two or more files is to make them become independent in each function.
However, it comes to be able to use the same variable by using sharing the variable.
The shade of meaning to make them become independent in each function weakens.
Therefore, use the argument and the return value of the function as much as possible.
Use sharing the variable only if it is necessary by all means.
Though it has been evaded here to be defined more than once by the use of the extern declaration
Actually, there is a method of preventing the repetition include of the header file, too.
Use # ifndef-#endif quasi-instruction for it.
#Only when a certain sign is not defined, the ifndef-#endif quasi-instruction :.
It is a sign of compiling the program placed between those.
The following header files can be made by using this character.
In this header file, whether sign _ INCLUDE_SUM _ is first defined is examined./* sum.h */ #ifndef _INCLUDE_SUM_ #define FONT> _INCLUDE_SUM_ int sum(int min,int max); #endif
It comes to never do the same declaration many times if it does in this manner.
Though only one can seem to be used since the second times in not being compiled
Finally, it is enough because all source files are united if it compiles once.
In general, combine extern declarations, and do as follows.
In addition, when such a comment is put, a better header file is completed.
/* sum.h */ #ifndef _INCLUDE_SUM_ #define FONT> _INCLUDE_SUM_ /* function that calculates the total value between min-max minimum value of int min int max maximum value return value Total of int value */ extern int sum(int min,int max); #endif
[ Place of explanation comment ]
See the program that applies the comment that explains the function well in this manner.
Even when the time that others saw and I see after passing of time if you do so
The content can be quickly understood and it is convenient.
However, the author should write such a comment in the header file.
I think that I should not write in the source file.
Though all the people who use the function read the header file
The purpose of the source file is for everyone not to necessarily read.
[ Do the nature that seems to be able to be done by the automatic operation ].
Because writing is decided and the header file is cut
Do the nature that can be automatically generated from the source file.
Actually, generate the declaration that corresponds to the header file in a lot of other languages automatically.
(Java makes the declaration from the class file from the automatic operation. )
However, there is a meaning of specification of the source file in the header file, too.
Previously make the header file, and program according to it.
Moreover, it is not necessary to write in the header file in the source file.
Because there is what for a peculiar function and variable to the source file are used well, too
There is an inconvenience, too, when even an unnecessary declaration is made a header by automatic generation.
Make the header file in C language for such reasons by hand power.
In the burial of the key word of the addition in the source file,
Automatic generation of the header file becomes possible.
If he or she challenges, how about an interesting person?