定義一個function.h檔案來宣告這些函數:
//#ifndef __FUNCTION_H__
//#define __FUNCTION_H__
int fun(int,int);
int times(int,int);
//#endif
接下來,在同一個function.c檔案中自定義這兩個函數:
#include "function.h"
int fun(int a,int b)
{
return a+b;
}
int times(int a,int b)
{
return a*b;
}
最後,如果要在另外一個檔案中,即main.c中呼叫這個函數,只需在程式開頭包含相應的標頭檔案即可。
# include "stdio.h"
# include "function.h" /*包含的標頭檔案*/
int main()
{
int a=2;
int b=4;
printf( "%d\n",fun(a,b));
printf( "%d\n",times(a,b));
return 0;
}
即在兩個互相呼叫的檔案裡新增一個頭檔案,加入需要呼叫的函數宣告即可。
但最好不新建標頭檔案,已有向上新增就行
總結:這樣的好處就是在同一個標頭檔案中能包含多個函數,在呼叫包含的函數時,直接可以呼叫。