Why use multiple files?
At that time, we have always written all programs in one screen.
This means that all programs have been written in a single file.
This method is simple and works well for programs that are small in scale.
But in a larger program, writing everything in one file would
It is difficult to know which program is where.
Furthermore, if several people are trying to create a single program, the
Since it is essentially impossible for two or more people to write to a single file at the same time, the
Programming with more than two people becomes virtually impossible.
The solution to this problem is to split the program into multiple files.
By splitting the program into multiple files, it is easier to see which program is where and
It also makes it possible for several people to create one program.
Source and Header Files
In fact, from the beginning, we unconsciously created programs by dividing them.
To use the printf function, etc., it was necessary to put #include <stdio.h> at the top.
In fact, this is exactly what splitting a program into multiple files means.
The #include pseudo-instruction is an instruction that takes in the contents of the specified file.
And stdio.h contains declarations for various functions, such as the printf function.
It is important to note that stdio.h contains only the declaration of the printf function, not the
The actual program is not written in stdio.h.
The actual program of the printf function is written in a separate file from stdio.h.
A file such as stdio.h, which contains only declarations, is called a header file.
It is customary to append .h as the extension to header files.
A file containing only function and variable declarations.
It is customary to use the extension .h.
In contrast, the file that actually describes the program is called a source file.
Everything we have described so far is a source file. Note that the extension is .c.
A file in which a program is written.
It is customary to use the extension .c.
Source and header files are usually created as a one-to-one correspondence.
Extract the declarations from the source file, and then
Create a header file with the same name (only change the extension).
Minimal header files
Here, we actually create a header file and a source file with a minimal configuration and
I would like to split the sum function created in Chapter 11.
First, let's create a source file, sum.c, that contains the sum function.
This is a simple matter of copying the sum function created in [Chapter 11](11-03,html).
/* sum.c */
int sum(int min, int max)
num = (min + max) * (max - min + 1) / 2;
Next, to make the functions contained in sum.c usable from other source files, we need to add
The declarations are extracted from sum.c and a header file sum.h is created.
Since the only declaration contained in sum.c is that of the sum function, this is written.
/* sum.h */
int sum(int min, int max);
This completes the division of the sum function.
However, this does not allow the program to run because there is no main function.
Therefore, create a source file containing the main function, main.c.
The description of main.c is almost the same as in Chapter 11, with one difference.
This time, the prototype declaration of the sum function is written in sum.h, so
Unless sum.h is imported, the sum function cannot be used.
Naturally, the #include pseudo instruction is used to import sum.h.
However, until now, we have enclosed the header file name in <>.
To import a header file you have created yourself, you are supposed to enclose it in "".
The inclusion of header files by the #include pseudo instruction is called " include.
To incorporate a header file with the #include pseudo instruction.
/* main.c */
value = sum(50, 100);
Now main.c is complete.
It is not necessary to create a header file for main.c or main.h.
This is because there is no need to use functions contained in main.c from other source files.
Now, I want to compile and run the program as soon as possible.
So far, the editor's functionality has automatically specified that a single file
If more than one file is used, you must specify which file to use.
Settings vary slightly depending on the compiler you are using.
Just add the file from the menu and it will be recognized automatically.
Just add the file to the project and it will be recognized automatically.
In the Compile Time Parameters field, enter
-emain main.c sum.c
Type "-emain main.c sum.c
-emain is an instruction to create an executable file named main.exe.
The latter two instructions both specify the name of the source file to be compiled.
The header file is included in the source file, so it is not necessary.
At that time, the compiler was automatically set up so that
There was no need to specify other files as in this case.
If you keep them in separate files like this, you can use the sum function without having to write it directly.
Just copy the files sum.h and sum.c and #include "sum.h" at the beginning.
All functions contained within sum.c are available.
It's cleaner than copying all the functions in sum.c to the same file, and
As long as you decide on the name and arguments of the function, you can have someone else create that function for you.
This allows for multi-person development by having the developer send us the file and we can compile it together later.
Well, we don't do that nowadays because we have Git as our only choice...
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.