C language learned by suffering
C language learned by suffering
The Stone of Division
Variable Sharing
In the previous section, we split the program into multiple files with minimal configuration.
However, only functions could be shared, and variables were not shared.
If you are developing your project in multiple source files, you can use the
In addition to functions, variables and constants will also need to be shared, but the
Variables cannot be shared in the way described in the previous section.
For example, if you declare a variable in a header file as follows
An error message is displayed, meaning that the declaration is a duplicate, and it cannot be compiled.
To understand this error more precisely, it is necessary to understand the meaning of the declaration.
So far, we've used the expression "declare," whether it's a function or a variable.
In fact, declarations have two functions.
When you declare a variable or function, the compiler remembers its name and form.
This is the function called declaration.
And at the same time, the compiler actually creates the variable or function.
This is the function called definition.
In the past, with variables, this declaration and definition were always done simultaneously.
Since declarations only tell the compiler what the variable or function should look like, the
As long as the form is the same, it does not matter how many times it is declared.
In a definition, however, you are creating the entity of a function or variable.
If the same function or variable is created multiple times, it will be indistinguishable and will result in an error.
However, only functions could be shared, and variables were not shared.
If you are developing your project in multiple source files, you can use the
In addition to functions, variables and constants will also need to be shared, but the
Variables cannot be shared in the way described in the previous section.
For example, if you declare a variable in a header file as follows
An error message is displayed, meaning that the declaration is a duplicate, and it cannot be compiled.
sum.h
/* sum.h */
int sum(int min, int max);
int Public;
To understand this error more precisely, it is necessary to understand the meaning of the declaration.
So far, we've used the expression "declare," whether it's a function or a variable.
In fact, declarations have two functions.
When you declare a variable or function, the compiler remembers its name and form.
This is the function called declaration.
And at the same time, the compiler actually creates the variable or function.
This is the function called definition.
In the past, with variables, this declaration and definition were always done simultaneously.
Since declarations only tell the compiler what the variable or function should look like, the
As long as the form is the same, it does not matter how many times it is declared.
In a definition, however, you are creating the entity of a function or variable.
If the same function or variable is created multiple times, it will be indistinguishable and will result in an error.
If it's a prototype declaration
In the previous section, I succeeded by writing only a prototype declaration.
This is because the prototype declaration only declares and does not define.
Therefore, any number of prototype declarations can be written (if written the same way).
This is because the prototype declaration only declares and does not define.
Therefore, any number of prototype declarations can be written (if written the same way).
extern declaration
In the previous section, we explained that a variable cannot be declared more than once because it is declared and defined at the same time.
To solve this problem, only the declaration needs to be done multiple times and the definition only once.
In this case, an extern declaration is provided that only declares the declaration.
The usage of the extern declaration is simple. Simply prefix your existing declarations with extern.
The following header file shows extern declarations for functions and variables.
This extern declaration allows variables to be shared by different source files.
The variable Public can now be shared by all source files that include sum.h.
However, this alone does not create the variable Public because it has not been defined.
So, create the entity by making an ordinary declaration in one source file somewhere.
The variable Public can now be used from both main.c and sum.c.
The following program is an example of it in action.
The result of executing this program is as follows
To solve this problem, only the declaration needs to be done multiple times and the definition only once.
In this case, an extern declaration is provided that only declares the declaration.
Keywords.
[EXTERN Declaration
A method of declaration in which only the declaration is made and no definition is provided.
The usage of the extern declaration is simple. Simply prefix your existing declarations with extern.
The following header file shows extern declarations for functions and variables.
extern declaration
/* sum.h */
extern int sum(int min, int max);
extern int Public;
This extern declaration allows variables to be shared by different source files.
First, declare some variable extern in a header file.
sum.h
/* sum.h */
extern int sum(int min, int max);
extern int Public; /* extern declaration of variables */
The variable Public can now be shared by all source files that include sum.h.
However, this alone does not create the variable Public because it has not been defined.
So, create the entity by making an ordinary declaration in one source file somewhere.
sum.c
/* sum.c */
int Public; /* create variable entity */
int sum(int min, int max)
{
int num;
num = (min + max) * (max - min + 1) / 2;
return num;
}
The variable Public can now be used 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 result of executing this program is as follows
Execution Result
100
Minimum required
Although variable sharing is a very useful technique, do not abuse it too much.
The original reason for splitting the file into multiple files is to make each function independent.
However, if variable sharing is used, the same variables can be used and
The significance of making each function independent is diminished.
Therefore, it is important to use function arguments and return values whenever possible, and to use the
Variable sharing should be used only when absolutely necessary.
The original reason for splitting the file into multiple files is to make each function independent.
However, if variable sharing is used, the same variables can be used and
The significance of making each function independent is diminished.
Therefore, it is important to use function arguments and return values whenever possible, and to use the
Variable sharing should be used only when absolutely necessary.
Prevent duplicate header files
So far, we have used the extern declaration to avoid duplicate definitions, but
In fact, there are ways to prevent duplicate header file inclusion itself.
To do so, use the #ifndef to #endif pseudo instructions.
The #ifndef to #endif pseudo-instructions only work if certain symbols are not defined.
The symbols are to compile the program between them.
This property can be used to create header files such as the following
In this header file, first check to see if the symbol _INCLUDE_SUM_ is defined, and then run the
Only if it is not defined, the subsequent program is compiled.
Here, the #define pseudo-instruction is used in the program that will be compiled later to
symbol_INCLUDE_SUM_ is defined so that if this header file is called a second time, the
The symbol _INCLUDE_SUM_ is already defined and will not compile.
In this way, the same declaration is not made many times.
It may seem that if it is not compiled after the second time, only one of them can be used.
Eventually all source files will be combined, so compiling once is sufficient.
Note that, in general, extern declarations are also combined as follows.
In addition, a better header file can be created by including comments such as these.
We recommend that this writing style be used at all times, as it is less likely to cause problems.
In fact, there are ways to prevent duplicate header file inclusion itself.
To do so, use the #ifndef to #endif pseudo instructions.
The #ifndef to #endif pseudo-instructions only work if certain symbols are not defined.
The symbols are to compile the program between them.
This property can be used 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, first check to see if the symbol _INCLUDE_SUM_ is defined, and then run the
Only if it is not defined, the subsequent program is compiled.
Here, the #define pseudo-instruction is used in the program that will be compiled later to
symbol_INCLUDE_SUM_ is defined so that if this header file is called a second time, the
The symbol _INCLUDE_SUM_ is already defined and will not compile.
In this way, the same declaration is not made many times.
It may seem that if it is not compiled after the second time, only one of them can be used.
Eventually all source files will be combined, so compiling once is sufficient.
Note that, in general, extern declarations are also combined as follows.
In addition, a better header file can be created by including comments such as these.
We recommend that this writing style be used at all times, as it is less likely to cause problems.
sum.h
/* sum.h */
#ifndef _INCLUDE_SUM_
#define _INCLUDE_SUM_.
/* Function to calculate the sum between min and max
int min Minimum value
int max max maximum value
Return value int Total value
*/
extern int sum(int min, int max);
#endif
Location of explanatory comments
We often see programs like this with comments explaining the function.
This way, when other people look at it, or even when you look at it after a long time
It is convenient to quickly grasp the contents.
However, the author believes that such comments should be placed in header files and not in
I do not think it should be in the source file.
The header file is read by everyone who uses the function, but
The reason is that not everyone reads the source files.
This way, when other people look at it, or even when you look at it after a long time
It is convenient to quickly grasp the contents.
However, the author believes that such comments should be placed in header files and not in
I do not think it should be in the source file.
The header file is read by everyone who uses the function, but
The reason is that not everyone reads the source files.
Seems like it could be done automatically...
Header files are written in a very standardized way.
It seems as if it could be generated automatically from the source file.
In fact, many other languages do not need header files because they do it automatically.
However, a header file also means a design document for the source files.
So you create the header file first, and then build the program accordingly.
Also, the source files contain information that does not need to be written in the header files, such as
Because functions and variables specific to a particular source file are often used, the
If automatic generation makes even unnecessary declarations into headers, a certain amount of waste will result.
The C programming language requires the programmer to be aware of many things, instead of just
If done consciously, this can greatly reduce waste.
It seems as if it could be generated automatically from the source file.
In fact, many other languages do not need header files because they do it automatically.
However, a header file also means a design document for the source files.
So you create the header file first, and then build the program accordingly.
Also, the source files contain information that does not need to be written in the header files, such as
Because functions and variables specific to a particular source file are often used, the
If automatic generation makes even unnecessary declarations into headers, a certain amount of waste will result.
The C programming language requires the programmer to be aware of many things, instead of just
If done consciously, this can greatly reduce waste.
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.