"#include“xxx.c”
1. Operation wave
Let's first experience whether a wave of #include "XXX. C" files can be used:
Reference Demo:
1//FileName :main
2#include 《stdio.h》
3#include 《stdlib.h》
four
5/***************************
6 *. C document declaration area
7**************************/
8#include “module1.c”
9#include “module2.c”
ten
11/***************************
12* Fuction: main
13 * Author: (last Bug)
14**************************/
15int main(int argc, char *argv[]) {
sixteen
17Fuction1;
18Fuction2;
19printf ("welcome to the official account: last bugn");
20return0;
21}
1//FileName: Module1.c
2#include《stdio.h》
3/***************************
4* Fuction: Fuction1
5 * Author: (last Bug)
6**************************/
7voidFuction1
8{
9printf( “Run Fuction1n”);
10}
1//FileName: Module2.c
2#include《stdio.h》
3/***************************
4* Fuction: Fuction2
5 * Author: (last Bug)
6**************************/
7voidFuction2
8{
9printf( “Run Fuction2n”);
10}
Output results:
Analyze:
It seems that this wave of operation is feasible, and the. H file seems to be omitted. As the bug bug said before, when analyzing the. H file, directly expand the. H file at the position in the corresponding. C file, and then further analyze it. In fact, this. C file is the same, and then look down.
Reference Demo:
1//FileName :main
2#include 《stdio.h》
3#include 《stdlib.h》
four
5char * cBug1 = “bugNo1”; // This is position 1
6char * cBug2 = “bugNo2”;
7/***************************
8 *. C document declaration area
9**************************/
10#include “module1.c”
11#include “module2.c”
twelve
13//char * cBug1 = “bugNo1”;// This is position 2
14//char * cBug2 = “bugNo2”;
fifteen
16/***************************
17* Fuction: main
18 * Author: (last Bug)
19**************************/
20int main(int argc, char *argv[]) {
twenty-one
22Fuction1;
23Fuction2;
24printf ("welcome to the official account: last bugn");
25return0;
26}
1//FileName: Module2.c
2#include《stdio.h》
3/***************************
4* Fuction: Fuction1
5 * Author: (last Bug)
6**************************/
7voidFuction1
8{
9printf( “Run Fuction1n”);
10printf( “%sn”,cBug1);
11}
1//FileName: Module2.c
2#include《stdio.h》
3/***************************
4* Fuction: Fuction2
5 * Author: (last Bug)
6**************************/
7voidFuction2
8{
9printf( “Run Fuction2n”);
10printf( “%sn”,cBug2);
11}
Output results:
Analyze:
We define two variables in position 1, and the above results are obtained after successful compilation and operation, which is in line with our expectations. However, when we remove the definition of position 1 and position 2, the program cannot be compiled. It seems that it is consistent with our expectation to directly expand the. C file in the compilation process.
2. What's the use?
In the long coding history of bug bacteria, this method is generally used only in two cases:
1. Maintain code without design
Some projects with a long history have been ravaged by more than n leaders. In fact, the code structure has been very terrible. Often, the content of each source file is very long. In order to keep the code as it is, we will use #include "XXX. C" to embed these related files for later maintenance.
2. Test code
In the early stage of software debugging, you may insert different test function functions in different files. In this way, you can easily introduce and eliminate them.
For example, you need to monitor some static variables in the source file, but you don't want to add test code in this file, so you can write test functions in #include "XXX. C" for use, such as:
1//FileName :main
2#include 《stdio.h》
3#include 《stdlib.h》
four
5staticint a = 5;
6/***************************
7 *. C document declaration area
8**************************/
9#include “module1.c”
ten
11/***************************
12* Fuction: main
13 * Author: (last Bug)
14**************************/
15int main(int argc, char *argv[]) {
sixteen
17Fuction1;
18printf( “main %dn”,a);
19printf ("welcome to the official account: last bugn");
20return0;
21}
1//FileName: Module2.c
2#include《stdio.h》
3/***************************
4* Fuction: Fuction1
5 * Author: (last Bug)
6**************************/
7voidFuction1
8{
9printf( “Run Fuction1n”);
10printf( “Fuction1 %dn”,a);
11}
Attention!!
Then, a little friend said, "the scope of static is only in the corresponding file". Using static a variables through multiple. C files above, the little partner's expression is not so appropriate!
3. Technical summary
In the normal development process, we still don't recommend using #include "XXX. C", because in the design process of our program, the. H file is an external reference interface, and. C is the corresponding internal implementation. If you abuse #include "XXX. C", it may cause repeated definitions of functions, etc., and also bring some troubles to debugging related programs, Of course, if you can do it well, there will be no problem.
However, it's a blessing for small partners who like to write long files. Divide a long. C file into multiple. C files, so that they can at least show their colleagues who don't know this usage!
two
void
1. Let's take a brief look at void
Void is only used in most small partner programs to pass in functions without parameters or return without types. However, the variables we usually define have specific types, such as int, float, char, etc. is there a variable of void type? You can experiment. The answer is: No, compilation will make mistakes.
It is obvious in the figure above that the compiler is not allowed to define variables of void type. Variables need to occupy a certain amount of memory. Since void indicates no type, the compiler naturally does not know how much memory to allocate, resulting in compilation failure.
Although void cannot directly modify variables, it can be used to modify the pointing of pointers, that is, the untyped pointer void *, which makes sense. The untyped pointer does not have to point to untyped data, but can point to any type of data.
2. Void * basic operation
In fact, you have encountered the use of void * when using dynamic memory allocation. Let's take a look at the prototype definitions of the following standard functions:
The above functions are related to memory operation. Some small partners may have used them and may not know the specific type of each parameter. The arguments passed in by the formal parameters of the void * part do not need forced type conversion, so there is no need to pay attention to the specific type pointed to by the incoming pointer, However, the void * returned by the function generally needs to be converted to the corresponding specific type through forced type, unless the last variable you pass is also void * type.
Reference void * usage:
1#include 《stdio.h》
2#include 《stdlib.h》
3#include 《malloc.h》
four
5#define NUM 10
6/*************************************
7 * fusion: learn about the use of void *
8 * Author: (last Bug)
9*************************************/
10int main(int argc, char *argv[]) {
11int *p1 = (int *)malloc(NUM*sizeof(int));
12int *p2 = (int *)malloc(NUM*sizeof(int));
13int i = 0;
fourteen
15 / / initialize P1
16for(i = 0; i 《 NUM; i++)
17{
18*(p1+i) = i;
19}
20 / / perform memory copy
21memcpy(p2,p1,NUM*sizeof(int));
twenty-two
23 / / output another allocated memory
24for(i = 0; i 《 NUM; i++)
25{
26printf( “%d,”,*(p2+i));
27}
28 / / free memory
29free(p1);
30free(p2);
31return0;
32}
Operation results:
3. Using void * to implement typeless data encapsulation
In order to maintain the integrity of the article, perhaps this is what the author most wants to introduce to you. Since void * is so flexible, it must be of great use. If it is only used to simply transfer parameters, it seems a little overqualified. We have to use it in the upper software design.
In some projects, it is often seen that small partners turn data types around, and sometimes even have to rewrite a function with only different data types for the change of a data type. Such code is tens of thousands of lines of code, According to the following example, we will introduce a method to reduce the increase of program duplicate code caused by data type changes.
Reference examples:
1#include 《stdio.h》
Our other product: