C language multi-file programming (you can also understand it)

I found that there are many children's shoes who have learned C language for a year and a half and still can't program with multiple files. Many people have only one source file (main.cpp or mian.c) for a program up to now. In fact, multiple files in C language are not mysterious. Today I will talk about how to program with multiple files. By the way, I will also summarize it myself.

Follow me step by step, it's very simple!

Now write a simple program. Make sure you can understand it if you have learned C language.

#include<stdio.h>
int max(int x, int y);
int min(int x, int y);
int main()
{
    
    
	int a = 10;
	int b = 20;
	printf("max=%d\n", max(a, b));
	printf("min=%d\n", min(a, b));
}
int max(int x, int y)
{
    
    
	return x > y ? x : y;
}
int min(int x, int y)
{
    
    
	return x < y ? x : y;
}

This procedure is simple enough!

All the function implementations of this program are in the same file, and the amount of code is small, so you can understand it. If the amount of code is a lot, you will find such program debugging very laborious.

In order to reflect the modular programming idea of ​​C language, can we put the declaration and implementation of the above two functions max and min in two files? The answer is yes.

Modified
main.c: put the main function
math.h: put the declaration of the function
math.c: put the implementation of the function

We can write the code in the following format

main.c file

#include<stdio.h>
#include"math.h"
int main()
{
    
    
	int a = 10;
	int b = 20;
	printf("max=%d\n", max(a, b));
	printf("min=%d\n", min(a, b));
}

math.h file

#pragma once
#ifndef MATH_H
#define MATH_H
int max(int x, int y);
int min(int x, int y);
#endif // !MATH_H

math.c file

int max(int x, int y)
{
    
    
	return x > y ? x : y;
}
int min(int x, int y)
{
    
    
	return x < y ? x : y;
}

Here I want to emphasize two points for everyone .
One . When we include the header file we wrote, we use ""

In fact, the #include mechanism is very simple, that is, directly copy the content of the file included in #include to the location of #include and replace the #include statement, so the main.c file and the following program are equivalent.

#include<stdio.h>
#pragma once
#ifndef MATH_H
#define MATH_H
int max(int x, int y);
int min(int x, int y);
#endif // !MATH_H
int main()
{
    
    
	int a = 10;
	int b = 20;
	printf("max=%d\n", max(a, b));
	printf("min=%d\n", min(a, b));
}

2.
Some friends may be a little unfamiliar with the following code

#ifndef MATH_H
#define MATH_H

#endif

In fact, this is for the header file not to be included repeatedly. Some careless students may include the header file twice, so as to avoid repeated inclusion

#ifndef MATH_H  // if not define
#define MATH_H  // define

#endif         // 结束

This code means that if we do not include the MATH_H header file, we will include the MATH_H header file.
If we include it twice, then in the second judgment, we have already included the MATH_H header file, and we will no longer include MATH_H Header file, so you can avoid repeated inclusion

Simple! A simple multi-file project is produced.

Of course, you don't need to do this when the amount of code is small, here is just for demonstration!

Guess you like

Origin blog.csdn.net/DR5200/article/details/112651558