- ..clause 1..: Reason to use two or more files
- ..clause 2..: Source and header file
- ..clause 3..: Minimum header file
Though it had written in the window in the editor up to now when the program was described
Because this method is simple, it is an effective method in a small-scale program.
However, in the program with a big scale when you write everything in one file
It becomes cramped which program where is.
In addition, it tries to make one program from some.
Two people or more's per file writing it at the same time : because it is basically impossible.
The programming by two people or more becomes impossible in fact.
It is necessary to write the program in the solution of this problem by dividing into two or more files.
It becomes comprehensible whether it divides into two or more files, and which program where is.
Moreover, making one program from some becomes possible.
Though it seems to be difficult if the program is divided into two or more files as for what
Actually, it programmed by dividing from the beginning in unconsciousness.
It was necessary to describe # include < stdio.h > at the head to use the printf function etc.
Actually, only this divides the program into two or more files.
#The include quasi-instruction is an instruction of taking the content of the specified file.
And, stdio.h contains the declaration of various functions of the printf function etc.
As for an important thing, only the declaration of the printf function is written in stdio.h here.
An actual program is to say not writing in stdio.h.
An actual program of the printf function is described in the file besides stdio.h.
The file to which only the declaration like stdio.h is written is called a header file.
It is a custom to apply .h as an extension for the header file.
On the other hand, the file that actually describes the program is called a source file.
[ Header file ]
File where only declaration of function and variable is described.
The extension. Making to h is a custom.
Make the source file and the header file to usually correspond by couple 1.
[ Source file ]
File where program is described.
The extension. Making to c is a custom.
In the preceding clause, it explained the meaning of the header file.
Here, make the header file and the source file by an actually minimum composition.
I want to divide the sum function made in Chapter 11.
First of all, make the source file and sum.c including the sum function.
This only copies the sum function made in Chapter 11 in an easy thing.
Next, to use the function included in sum.c from other source files/* sum.c */ int sum(int min,int max) { int num; num = (min + max) * (max - min + 1) / 2; return num; }
The division of the sum function was completed by this./* sum.h */ int sum(int min,int max);
The description of main.c also almost has a point different by one though it is the same as Chapter 11.
This time, because the prototype declaration of the sum function is described in sum.h
The sum function cannot be used as long as sum.h is not taken.
Naturally, use # include quasi-instruction to take sum.h.
However, the header file name up to now Though it enclosed with <>
To take the header file made for myself, it is supposed to enclose it with ""
It is called include to take the header file by # include quasi-instruction.
[ Include ]
#Take the header file by the include quasi-instruction.
Main.c is completed by this, too./* main.c */ #include <stdio.h> # include "sum.h" int main(void) { int value; value = sum(50,100); printf("%d\n",value); return 0; }
The header file and main.h of main.c need not be made.
It is because the function included in main.c need not be used from other source files.
Well, it will not be possible to compile the way things are going though it compiles immediately and it wants to execute it.
Though it has been automatically specified up to now by the function of the editor if it is one file
When two files or more are used, it is necessary to specify the file used.
The setting is somewhat different depending on the compiler used.
With Borland C++ Compiler The setting of Visual C++ Toolkit 2003.
Menu-> execution-> Select the parameter when you compile.
To the column of the parameter when compiling
-emain main.c sum.cIt drives it.
- What is emain of the execution file It is an instruction to make by the name called main.exe.
Both compiled source file names are specified at two of the latter halfs.
Because the header file does include by the source file, it is unnecessary.
Setting of LSI C-86Compile immediately when you finish setting.
Menu-> execution-> Select the parameter when you compile.
To the column of the parameter when compiling
-o main.exe main.c sum.cIt drives it.
- What is o main.exe of the execution file It is an instruction to make by the name called main.exe.
Both compiled source file names are specified at two of the latter halfs.
Because the header file does include by the source file, it is unnecessary.
[ It sets and it is unnecessary at stdio.h ].
Though it had divided into two or more files by stdio.h etc. before in unconsciousness
Because the compiler automatically set it at that time
There were no necessity etc. the specification of other files like this time.
Even if you do not write the sum function directly if you divide into another file in this manner
[ Case editor ]
This time, though it compiles purposely specifying the file
It goes in full automation when the editor for development real (professional specification) is used.
Other various functions are attached, and it is thorough, and it is very comfortable.
Made by Microsoft Corporation the editor for the program development for Windows VisualC++ is standard.
The person who wants to study seriously may examine it because it can buy it if it is a learning crack by the price which 10,000 yen is under.