免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 7621 | 回复: 30
打印 上一主题 下一主题

c 语言 头文件 存在的 好处以及意义?谁能把我搞懂 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-11 10:37 |只看该作者 |倒序浏览
如题。下面例子我不用头文件一样玩呀。根本没有体现头文件的必要性质呀。

tst.c
int add(int x, int y)
{
int z;
z = x +y;
return z;
}
a_call_tst_.c

int  a_add(int x, int y)
{
return add(x,y);
}

b_call_tst_.c

int  b_add(int x, int y)
{
return add(x,y);
}

main.c
#include<stdio.h>
int  main()
{
printf("sum of a_add %d \r\n",a_add(10,10));
printf("sum of b_add %d \r\n",b_add(20,20));
return 1;
}

makefile:
result:main.o
        cc  main.o a_call_tst_.o b_call_tst_.o tst.o  -o result
main.o:a_call_tst_.o b_call_tst_.o
        cc -c main.c
a_call_tst_.o:  tst.o
        cc -c a_call_tst_.c
b_call_tst_.o:  tst.o
        cc -c b_call_tst_.c
tst.o:tst.c
        cc -c tst.c  
clean:
        rm -rf *.o

[ 本帖最后由 ronghuahan 于 2008-1-11 11:28 编辑 ]

main.rar

643 Bytes, 下载次数: 39

论坛徽章:
0
2 [报告]
发表于 2008-01-11 10:48 |只看该作者
这东西很广的吧.
1. 比如有时候你写的代码只是提供一个函数接口, 具体的实现作为二进制库来调用的时候.
2. 可以加强类型的安全检查 .
楼下继续补充.  

论坛徽章:
0
3 [报告]
发表于 2008-01-11 10:56 |只看该作者
养眼,方便

论坛徽章:
0
4 [报告]
发表于 2008-01-11 10:58 |只看该作者
补充:比如象QT可以用可视工具产生一个.h来定义一个窗体,auto tools可以生成一个config.h来定制系统的相关编译选项
还可以便于preprocess,提高可读性,反正我觉得pascal那种乱糟糟写在一起的代码很恶心

论坛徽章:
0
5 [报告]
发表于 2008-01-11 11:03 |只看该作者
头文件,恩,很好,很强大,很黄,很暴力

论坛徽章:
0
6 [报告]
发表于 2008-01-11 11:10 |只看该作者
穿衣服的好处及意义?

论坛徽章:
0
7 [报告]
发表于 2008-01-11 11:14 |只看该作者
原帖由 cugb_cat 于 2008-1-11 11:03 发表
头文件,恩,很好,很强大,很黄,很暴力

“很。。,很。。。。”这年月流行的咧

论坛徽章:
0
8 [报告]
发表于 2008-01-11 11:54 |只看该作者
主要是维护方便:
如果不用头文件在a_call_tst_.c b_call_tst_.c中,
采取直接声明的 struct bbb_s,如果struct bbb_s 变动的话,将来要修改多份副本!
如果用头文件就直接修改头文件好了!

很大,很黄,很暴力!

tst.h

struct bbb_s
{
int  x;
};
typedef struct bbb_s bbb_t;

tst.c
#include "tst.h"
int add(bbb_t a, bbb_t b )
{
int z;
z =a.x + b.x;
return z;
}

a_call_tst_.c

#if 0
struct bbb_s
{
int x;
};
typedef struct bbb_s bbb_t;
#endif
#include "tst.h"
int  a_add(int x, int y)
{
bbb_t a,b;
a.x = x;
b.x =y;
return add(a,b);
}

b_call_tst_.c
#if 0
struct bbb_s
{
int x;
};
typedef struct bbb_s bbb_t;
#endif
#include "tst.h"
int  b_add(int x, int y)
{
bbb_t a,b;
a.x = x;
b.x = y;
return add(a,b);
}

main.c
#include<stdio.h>
int  main()
{
printf("sum of a_add %d \r\n",a_add(10,10));
printf("sum of b_add %d \r\n",b_add(20,20));
return 1;
}
makefile:
result:main.o
        cc  main.o a_call_tst_.o b_call_tst_.o tst.o  -o result
main.o:a_call_tst_.o b_call_tst_.o
        cc -c main.c
a_call_tst_.o:  tst.o tst.h
        cc -c a_call_tst_.c
b_call_tst_.o:  tst.o tst.h
        cc -c b_call_tst_.c
tst.o:tst.c tst.h
        cc -c tst.c  
clean:
        rm -rf *.o

[ 本帖最后由 ronghuahan 于 2008-1-11 12:05 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-01-11 12:00 |只看该作者
很大,很黄,很暴力!

[ 本帖最后由 ronghuahan 于 2008-1-11 12:05 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-01-11 12:06 |只看该作者
楼主的题目有意思: 谁能把我搞懂
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP