【C语言】头文件,多文件编辑
1.多文件编辑,一个工程包含多个源代码
2.除了主函数,给其他几个函数分配头文件声明
程序:
main.c:
#include <stdio.h>
#include "a.h"
//#include "func.c"
/* #include "func.c"声明在“build”之后添加可以,在“build”之前添加会报错func.obj : error LNK2005: _add already defined in age1.obj
Debug/age1.exe : fatal error LNK1169: one or more multiply defined symbols found*/
int main()
{
int n ;
n= add();
printf("n:%d\n", n);
return 0;
}
a.h:
/* 头文件声明*/
#ifndef __a_H__//如果一个文件包含了此头文件多次,使用这种方法,即在第一次编译时没有定义ABC的宏,执行了下面的所有,第二次再遇到编译此文件时ABC已经被定义,就不会再编译
// 使用条件编译语句#ifndef可以避免重复编译,
// 不同头文件的宏名称必须不同
#define __a_H__
struct cps
{
int num;
int num1;
} ; //头文件可以放结构体的声明,但不能有结构体的定义,不然会报错重复定义(error link2005)
int add();//头文件里至少要包含配对源文件里所有函数的声明
#endif //__a_H__
func.c:
/*函数功能实现*/
#include "a.h"//将声明本函数的头文件包含进来
//如果还有其他函数也需要包含进
//#include <stdio.h> //这个头文件写不写都可以,不写会有“warning c4013”警告,不会报错
int add()
{
struct cps
{
int num;
int num1;
} student={0,0};
printf("num:");
scanf("%d",&student.num);
printf("num1:");
scanf("%d",&student.num1);
return student.num +student.num1;
}
程序正常可用。
还没有评论,来说两句吧...