MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Minimal partitioning

Reasons for Using Multiple Files
So far, I've been writing code directly into the editor window.
"We've always written all our programs within a single screen at once."
This means we've been writing all the programs in a single file.

This method is simple, so it's effective for small-scale programs.
However, in large programs, writing everything to a single file...
It becomes difficult to know where each program is located.
Furthermore, when attempting to create a program with multiple people,
It's fundamentally impossible for more than two people to write to the same file simultaneously.
Programming with more than two people becomes practically impossible.

To solve this problem, you need to write a program that is split across multiple files.
Splitting the files makes it easier to see where each program is located.
It will also be possible to create a program with multiple people.
Source and header files
It seems like splitting the program into multiple files would be quite complicated, doesn't it?
Actually, I was subconsciously segmenting and programming from the very beginning.

To use functions like printf, you needed to include #include at the beginning.
Actually, this is what it means to split a program into multiple files.

The #include pseudo-instruction is a command to incorporate the contents of a specified file.
And stdio.h contains declarations for various functions, such as printf.
The important thing to note here is that stdio.h only contains the declaration of the printf function.
The point is that the actual program isn't written within stdio.h.
The actual program for the printf function is written in a file separate from stdio.h.

Files containing only declarations, such as stdio.h, are called header files.
It is customary to use the file extension .h for header files.

キーワード
【header file】

functionやVariable declarationのみが記述されたFile。
拡張子は .h にすることが慣習となっている。


In contrast, the files in which you actually write the program are called source files.
Everything described so far has been source files.Also, the file extension will be .c.

キーワード
【Source file】

プログラムが記述されたFile。
拡張子は .c にすることが慣習となっている。


Source files and header files are typically created with a one-to-one correspondence.
Extract the declarations from the source files.
Create a header file with the same name (just changing the extension).
Minimal header files
In the preceding section, we explained the meaning of header files.
Here, we will actually create a header file and a source file with the most basic structure.
第11章で作成したsumfunctionをminute割してみたいと思います。

Let's start by creating a source file, sum.c, that includes the sum function.
This is simple; just copy the sum function created in [Chapter 11](11-03.html).

sum.c
/* sum.c */
int sum(int min, int max)
{
    int num;
    num = (min + max) * (max - min + 1) / 2;
    return num;
}

Next, to make the functions contained within sum.c accessible from other source files,
Extract the declarations from sum.c and create a header file named sum.h.
The declarations contained within sum.c are only the declaration of the sum function, so I will write that here.

sum.h
/* sum.h */
int sum(int min, int max);

We have now completed the partitioning of the sum function.
However, this doesn't have a main function, so it cannot be executed as a program.
Therefore, we will create a source file containing the main function, main.c.

The code in main.c is largely the same as Chapter 11, but there is one difference.
This time, the prototype declaration for the sum function is written in sum.h.
You cannot use the sum function unless you include sum.h.

Of course, we use the `#include` pseudo-instruction to include sum.h.
However, previously, header file names were enclosed in <>.
To include a header file you created, it needs to be enclosed in double quotes.
Including header files with the #include pseudo-instruction is referred to as including.

キーワード
【included】

Including header files with the #include preprocessor directive.



main.c
/* main.c */
#include "sum.h"
#include <stdio.h>

int main(void)
{
    int value;
    value = sum(50, 100);
    printf("%d\n", value);
    return 0;
}

That completes main.c as well.

Please note that you do not need to create a header file, main.h, for main.c.
Because the functions contained in main.c don't need to be used from other source files.

Well, I'd like to compile and run it right away, but as it is, it won't compile.
Previously, the file was automatically specified based on the editor's functionality.
When using two or more files, you must specify which files to use.
The configuration may vary slightly depending on the compiler being used.


Setting up a C Development Environment for Learning
No special setup is required.
Adding files through the menu will automatically register them.



Visual Studio Settings
No special setup is required.
Adding files to the project will automatically make them recognized.



Configuring the Borland C++ Compiler and Visual C++ Toolkit 2003
メニュー -> run -> コンパイルHourパラメータ を選択し、
コンパイルHourパラメータの欄に、
-emain main.c sum.c
and type it in.
`-emain` is an instruction that creates an executable file named `main.exe`.
The last two specify source file names to be compiled together.
The header file is unnecessary because it's included in the source file.



No configuration is required when using stdio.h.
今までにも、stdio.h などにより無意識の内に複数Fileにminute割していたのですが、
そのHourは、Compilerがautomatic的に設定していたので、
今回のように他のFileを指定する必要などはありませんでした。


"By separating it into another file like this, you won't need to write the sum function directly."
Just copy the sum.h and sum.c files, and simply include "#include "sum.h"" at the top.
All functions contained within sum.c are available.

It's cleaner than copying functions from within sum.c to the same file,
If you just decide on the function's name and arguments, you can have someone else create it for you.
"You can send me the file, and we can compile it together, enabling multi-person development."
まあ、現代ではGit一択ですので、そんなことはしないのですが・・・


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19