作者james732 (好人超)
站内C_and_CPP
标题Re: [问题] C语言---要怎麽写标头档阿??
时间Sun Apr 5 20:37:33 2009
假设你的 header file 叫做 add.h
#ifndef _ADD_H
#define _ADD_H
int add(int, int);
#endif
//add.h 到这里结束
请注意,在 header file 里,只要写函式的宣告 (prototype)
而函式的定义(definition) 请写在 source file 里。
一般来说,source file 会与 header file 同名称,但副档名为 c
add.c
#include "add.h"
int add(int a, int b)
{
return a + b;
}
// add.c 到这里结束
然後你的 main.c,就只要 include 该 header file
main.c
// 其他你会用到的 header file, 譬如 stdio.h 等
#include "add.h"
int main()
{
printf("%d\n", add(3, 5));
}
// main.c 到这里结束
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.104.63.227
1F:推 cosLove:学到了 感谢你的文章 04/05 21:25
2F:→ wupojung:推....认真... 04/06 00:49
3F:推 as123429:不好意思,我发现把header file跟source file都写在同一 04/06 22:48
4F:→ as123429:档案 xxx.h 也可以耶,为什麽?会有什麽差异吗? 04/06 22:49
5F:推 po953:楼上那样也可以 但是不好维护 04/07 09:14
6F:推 kenstay:学到东西了,推一个 04/07 10:23
7F:推 sunneo:除非有要inline或者写template lib,不然写在.h实在不是好事 04/07 10:50