第一种方式,直接在.h文件中编写函数
1.编写sum.h
int add(int a,int b){
return a+b;
}
2.主函数中引用
#include <stdio.h>
#include "sum.h"
void main() {
int num=add(1,1);
printf("d",num);//输出:2
}
#include "sum.h":先从项目当前路径寻找sum.h文件,找不到再到编译器的内置函数库中寻找
#include <sum.h>:先从编译器的内置函数库中寻找,找不到再到项目当前路径寻找sum.h文件
第二种方式,声明和实现类分开
1.编写sum.h
#ifndef FUN1_H //if判定,若是此宏已经定义过(此头文件已经实现过),则跳过
#define FUN1_H //如果没定义,就进入代码块编译
int add(int,int);
#endif
2.编写sum.c
#include "sum.h" //绑定到sum.h,表示此文件是sum.h的实现
int add(int num1,int num2){
return num1+num2;
}
3.主函数中引用
#include <stdio.h>
#include "sum.h"
void main() {
int num=add(1,1);
printf("d",num);//输出:2
}