C语言如何写头文件?

C语言如何写头文件?,第1张

/*头文件内容,假设名字是test.h*/

#ifndef

MYHEADFILE

#define

MYHEADFILE

void

InitInterpolation()

void

Draw_Border()

void

Draw_Background()

void

Draw_Gray()

#endif

/*以下是test.c的内容*/

#include

"test.h"

/*后面就是各个函数的实现*/

同项目中其他各个文件需要使用这些函数时只需要下面这样一句:

#include

"test.h"

千万不要包含.c文件,会出现重复定义问题

头文件内容

#define PI 3.14159

float mianji(float r)

{

return PI*r*r

}

保存名称为yuanmianji.h

实现文件内容

#include <stdio.h>

#include "yuanmianji.h"

int main(void)

{

float r

printf("请输入圆的半径: ")

scanf("%f",&r)

printf("圆的面积是: %.2f\n",mianji(r))

return 0

}

运行截图如下:

/*头文件内容,假设名字是test.h*/

#ifndef MYHEADFILE

#define MYHEADFILE

void InitInterpolation()

void Draw_Border()

void Draw_Background()

void Draw_Gray()

#endif

/*以下是test.c的内容*/

#include "test.h"

/*后面就是各个函数的实现*/

头文件一般用于多个源码的工程,当然,单源码可以写头文件,这个只是一种风格或习惯,一般是程序的声明部分写在.h中,如你的

char mainmenu(void)

char getBookType (void)

char bookItem (void)

int getBookNumber(void)

还有就是fiction,nonFiction的声明,可写成

extern int fiction

extern int nonfiction


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11643315.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存