第1个回答 2012-06-28
头文件一般存放函数或数据的说明。对应的c文件为具体实现,当然,也可以写在一起。如:
compare.h文件代码:
#include<stdio.h>
int compare(int a,int b){ if(a>b) printf("%d is biger than %d \n",a,b); if(a==b) printf("%d is equal %d \n",a,b); if(a<b) printf("%d is smaller than %d \n",a,b); return 0;}
主函数代码:
#include "compare.h"
int main(int argc, char *argv[]){ int a=12,b=13; compare(a,b); system("PAUSE"); return 0;}本回答被提问者和网友采纳
第2个回答 2012-06-28
如果想偷懒,可以把代码都写到头文件中,规范的做法是头文件声明,C文件实现。贴一个偷懒的代码。
头文件:
#if !defined(__MYINC_H)
#define __MYINC_H
#ifdef __cplusplus
extern "C" {
#endif
int comp(int a, int b)
{
if(a>b) return 1;
else if (a<b) return -1;
else return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* __MYINC_H */
主程序:
#include <stdio.h>
#include "myinc.h"
int main()
{
int a, b;
scanf("%d, %d", &a, &b);
switch(comp(a, b))
{
case -1: printf("a<b\n"); break;
case 0 : printf("a=b\n"); break;
case 1 : printf("a>b\n"); break;
}
return 0;
}本回答被网友采纳
第3个回答 2012-06-28
新建一个头文件和它的源文件,在头文件中声明函数compare(),在源文件中写compare()的具体代码。
要比较两个数,要看是什么类型的数了,如果是整型,直接判断谁大谁小,如果是字符串之类的,就用strcmp。自己根据情况写吧
第4个回答 2019-02-12
得把多个放在一个工程中啊,同时用到别的c文件里的函数时要用extern声明。