定義一個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;
}
即在兩個互相調用的文件里添加一個頭文件,加入需要調用的函數聲明即可
但最好不新建頭文件,已有的上添加
總結:這樣的好處就是在同一個頭文件中能包含多個函數,在調用包含的函數時,直接可以調用。
