应用程序和驱动程序调用自己定义的函数
2016-07-27 17:26
375 查看
1.应用程序调用自己定义的函数
1.1第一种方法
定义两个函数add.c和jian.c:/*add.c*/
#include<stdio.h> int add(int x,int y) { return x+y; }
/*jian.c*/ #include<stdio.h> int jian(int x,int y) { return x-y }定义一个main.c函数
</pre><pre name="code" class="cpp">/*main.c*/ #include<stdio.h> <pre name="code" class="csharp">extern int add (int, int);//extern的目的是申明函数或者变量可以在本模块或者其他模块中使用 extern int jian(int,int);int main(){ int result1=0;result2=0; result1=add(2,5); printf("result1=%d \n",result1); result2=jian(10,2); printf("result2=%d\n,result2");}然后编译文件,生成可执行文件test:gcc add.c jian.c main.c -o test.
1.2第二种方法
同样定义两个函数add.c和jian.c如上所示,然后定义一个头文件cal.h:/*cal.h*/
extern int add (int, int);//extern的目的是申明函数或者变量可以在本模块或者其他模块中使用extern int jian(int,int);此时的主函数:main.c
/*main.c*/#include<stdio.h>#include“cal.h”//双引号代表在本目录下搜寻,单括号代表在头文件库种搜寻int main(){int result1=0;result2=0;result1=add(2,5);printf("result1=%d \n",result1);result2=jian(10,2);printf("result2=%d\n,result2");}然后,如上面一样的编译。
相关文章推荐
- Activity的缓存方法
- HDU 2717 Catch That Cow【BFS】
- 脚本编写
- 计算机网络基础知识总结
- 集合详解(三)----Map的两种遍历方式
- 深入理解Java中的容器
- 堆排序
- Linux 之 信号
- Redhat Enterprise 7.2 安装 MySQL 5.7
- QA问答系统
- 基于tcp的c/s模型的一个简单的socket实现
- Oracle SQL优化 总结(很全面)一起来学习一下!
- Android源码编译并预装带JNI的apk
- mongoDB mapReduce
- 快速排序法
- UNIX下的LD_PRELOAD环境变量
- (转) Lua string 操作函数
- Android Repo的manifest XML文件格式
- 关于Windows cmd下目录带空格的处理办法
- HDU 1003 Max Sum(最大连续子列和)