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

The Divide Principle

Variable sharing
In the previous section, we divided the program into multiple files with a minimal structure.
However, we could only share functions; we did not share variables.

If you develop using multiple source files,
"However, there will also be a need to share variables and constants, not just functions."
"The method in the previous chapter does not allow for sharing variables."

For example, if you declare a variable within a header file, like this:
"I'm getting an error saying 'declaration is redundant' and it won't compile."

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

To better understand this error, you need to understand the meaning of declarations.
So far, we've been using the term "declare" to describe both functions and variables.
Actually, declarations have two kinds of functionality.

When variables or functions are declared, the compiler memorizes their names and shapes.
This is a feature called the Declaration.
And simultaneously, the compiler actually creates variables and functions.
This is a function called definition.
"Previously, we always performed declaration and definition simultaneously with these variables."

Declarations merely inform the compiler about the form of variables and functions.
As long as the form is the same, it doesn't matter how many times you declare it.
However, in terms of the definition, this creates concrete instances of functions and variables.
It results in an error because it becomes difficult to distinguish between multiple instances of the same function or variable.

Prototype declaration, if so.
前章では、Prototype Declarationだけを記述して成功しましたが、
これは、Prototype Declarationは、宣言だけを行い、定義は行わないからis.
Therefore,Prototype Declarationは(同じ書き方なら)いくつでも書けます。

External declaration
"The preceding section explained that variables cannot be declared multiple times because declaration and definition occur simultaneously."
To resolve this issue, you need to declare things multiple times and define them only once.
In that case, a extern declaration is provided for simply declaring it.

キーワード
【External declaration】

Declaration without definition.


The usage of extern declarations is straightforward."It's just a matter of preceding previous declarations with the keyword 'extern'."
The following header file contains extern declarations for functions and variables.

External declaration
/* sum.h */
extern int sum(int min, int max);
extern int Public;

"This extern declaration allows you to share variables between different source files."

First, declare some variables as extern within a header file.

sum.h
/* sum.h */
extern int sum(int min, int max);
extern int Public; /* variableのExternal declaration */

Now the variable Public can be shared across all source files that include sum.h.
However, since it is not defined, the variable Public has not been created.
"Therefore, you create an instance by performing a regular declaration within one source file."

sum.c
/* sum.c */
int Public; /* variableの実体の作成 */

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

With this, the variable Public will be accessible from both main.c and sum.c.
The following program is an example of it in action.

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

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


sum.c
/* sum.c */
int Public;

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

The output of this program is as follows:

Execution results
100


to a minimum
Variable sharingは大変便利なテクニックですが、あまり乱用しないでください。
本来、複数のFileにminute割するのは、機能毎に独立させるためis.
しかし、Variable sharingを使用すると、同じvariableが使えるようになってしまい、
機能毎に独立させる意味合いが薄れてしまいます。

Therefore,可能な限りfunctionのargumentやreturn valueを利用し、
Variable sharingは、absolutely necessaryな場合にのみ使用してください。

Header file inclusion prevention
So far, we have used extern declarations to avoid redundant definitions, but...
Actually, there is a way to prevent header file double inclusion itself.
We will use the #ifndef~#endif conditional directive for that.

The `#ifndef~#endif` pseudo-instructions are used only when a symbol is not defined.
It's a notation indicating the compilation of the program enclosed in between.
This property can be leveraged to create header files such as the following.

sum.h
/* sum.h */
#ifndef _INCLUDE_SUM_
#define _INCLUDE_SUM_

int sum(int min, int max);

#endif

In this header file, we first check if the symbol _INCLUDE_SUM_ is defined.
Compile the subsequent program only if it was not previously defined.
Here, using the #define pseudo-instruction within a program that will be compiled later,
"Since the symbol _INCLUDE_SUM_ is defined, if this header file is included for the second time,"
The symbol _INCLUDE_SUM_ is already defined, and compilation will not proceed.

This way, you won't have to repeat the same declaration multiple times.
"It seems like it might only be usable once, as it won't be compiled again afterward."
Ultimately, all source files will be combined, so a single compilation is sufficient.

In general, this is often combined with an extern declaration as follows.
Furthermore, including comments like these will result in a more complete header file.
I recommend always using this writing style, as it minimizes the likelihood of issues.

sum.h
/* sum.h */
#ifndef _INCLUDE_SUM_
#define _INCLUDE_SUM_

/* min~max間の合計値を計算するfunction
int min 最小値
int max 最大値
return value int 合計値
*/
extern int sum(int min, int max);

#endif


Location of explanatory comments
この様に、functionをDescriptionするcommentをつけるプログラムは良く見かけます。
こうすれば、他人が見たHourや、Timeがたってから自minuteが見たHourでも、
内容を素早く把握できて便利is.

ただ、筆者は、この様なcommentはheader fileの中に書くべきで、
Source fileの中に書くべきではないと思います。
header fileはそのfunctionを利用するすべての人が読みますが、
Source fileの方は誰もが読むとは限らないためis.


It feels like it could be automated, though...
header fileは書き方が決まり切っているため、
Source fileからautomatic的に生成することができるような気もします。
実際、他の多くの言語では、automaticでやってくれるため、header fileは不要is.

しかし、header fileにはSource fileの設計書という意味もあります。
先にheader fileを作り、それに合わせてプログラムを作っていくわけis.
また、Source fileには、header fileに書く必要のない、
そのSource file固有のfunctionやvariableが使われていることも良くあるため、
automatic生成で不要な宣言までヘッダーにしてしまうと、ある種のムダが出てしまいます。

C languageはプログラマーが意識しなければならないことが多いかわりに、
意識して行えば、ムダを大きく減らせるようになっています。



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