Sub-file programming of Linux library

Today is New Year’s Eve. I posted a morning couplet. After lunch
, I’ll learn a little bit. The evening is Happy Night. After New Year’s Eve, you will rebirth from Nirvana and come back against the wind. This sentence is a win!
Yes, Douyin saw it. . .


Too much nonsense, let’s get to the point:

Benefits of file-by-file programming:

        Different functional modules can be assigned to different people.

  • The internet
  • Thread
  • Motor
  • Ultrasound
  • ···

benefit:

  • Functional responsibility division
  • Easy to debug
  • The main program is concise

Example:

A code containing custom functions and main functions

#include <stdio.h>

int add(int x,int y)
{
    
    
	return x+y;
}

int min(int x,int y)
{
    
    
	return x-y;
}

int mul(int x,int y)
{
    
    
	return x*y;	
}

int div(int x,int y)
{
    
    
	return (float)x/y;
}

int main()
{
    
    
	int data1;
	int data2;
	int ret;
	float ret_float;
	printf("请输入数字1:\n");
	scanf("%d",&data1);
	printf("请输入数字2:\n");
	scanf("%d",&data2);
	printf("结果为:\n");
	ret = add(data1,data2);
	printf("%d + %d = %d\n",data1,data2,ret);
	ret = min(data1,data2);
	printf("%d - %d = %d\n",data1,data2,ret);
	ret = mul(data1,data2);
	printf("%d * %d = %d\n",data1,data2,ret);
	ret_float = div(data1,data2);
	printf("%d / %d = %f\n",data1,data2,ret_float);

	return 0;
}

normal operation:
Insert picture description here


After the file is divided:

caclate_T.c

#include <stdio.h>


int main()
{
    
    
	int data1;
	int data2;
	int ret;
	float ret_float;
	printf("请输入数字1:\n");
	scanf("%d",&data1);
	printf("请输入数字2:\n");
	scanf("%d",&data2);
	printf("结果为:\n");
	ret = add(data1,data2);
	printf("%d + %d = %d\n",data1,data2,ret);
	ret = min(data1,data2);
	printf("%d - %d = %d\n",data1,data2,ret);
	ret = mul(data1,data2);
	printf("%d * %d = %d\n",data1,data2,ret);
	ret_float = div(data1,data2);
	printf("%d / %d = %f\n",data1,data2,ret_float);

	return 0;
}

caclate_F.c


int add(int x,int y)
{
    
    
	return x+y;
}

int min(int x,int y)
{
    
    
	return x-y;
}

int mul(int x,int y)
{
    
    
	return x*y;	
}

int div(int x,int y)
{
    
    
	return (float)x/y;
}

caclate_F.h


int add(int x,int y);	//注意有分号

int min(int x,int y);

int mul(int x,int y);

int div(int x,int y);


Write caclateF.h in caclate_T.c and name it caclate_T2.c

#include <stdio.h>
#include "caclate_F.h"

int main()
{
    
    
	int data1;
	int data2;
	int ret;
	float ret_float;
	printf("请输入数字1:\n");
	scanf("%d",&data1);
	printf("请输入数字2:\n");
	scanf("%d",&data2);
	printf("结果为:\n");
	ret = add(data1,data2);
	printf("%d + %d = %d\n",data1,data2,ret);
	ret = min(data1,data2);
	printf("%d - %d = %d\n",data1,data2,ret);
	ret = mul(data1,data2);
	printf("%d * %d = %d\n",data1,data2,ret);
	ret_float = div(data1,data2);
	printf("%d / %d = %f\n",data1,data2,ret_float);

	return 0;
}

Enter in the command line:

gcc caclate_F.c caclate_T2.c

It also runs successfully:
Insert picture description here


The difference between <> and "" in the header file

<> Is the priority to search and import from the /usr/include/ folder.
Insert picture description here

"" Is preferentially search and import from the current folder.

Guess you like

Origin blog.csdn.net/zouchengzhi1021/article/details/113789747