DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

C语言中使用其他文件中的函数


例如main.c的main()方法中用myfunc.c中的mysub()函数,首先要在myfunc.c中实现mysub:

int mysub(int a, int b) {

return a-b; }

之后main.c有两种方法引用:

  • 声明函数原型

#include

#include int mysub(int a, int b); int main() { printf("Hello world!\n"); int c = mysub(3,5); printf("result is: %d",c); return 0; }

  • 通过头文件引用

将main.c中的"int mysub(int a, int b);"拷贝到myfunc.h文件中,然后将main.c中的"int mysub(int a, int b);"改为:#include "myfunc.h"。

Code::Blocks中生成头文件

选中要生成头文件的C文件,主菜单 -> Edit -> Swap header/source。



Published

Dec 18, 2012

Last Updated

Dec 18, 2012

Category

Tech

Tags

  • C 12

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor