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.
"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;
}
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
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.