admin 管理员组

文章数量: 1184232


2024年2月19日发(作者:异步fifo中写用量和写用量)

C语言头文件作用及写法

头文件几个好处:

1,头文件可以定义所用的函数列表,方便查阅你可以调用的函数;

2,头文件可以定义很多宏定义,就是一些全局静态变量的定义,在这样的情况下,只要修改头文件的内容,程序就可以做相应的修改,不用亲自跑到繁琐的代码内去搜索。

3,头文件只是声明,不占内存空间,要知道其执行过程,要看你头文件所申明的函数是在哪个.c文件里定义的,才知道。

4,他并不是C自带的,可以不用。

5,调用了头文件,就等于赋予了调用某些函数的权限,如果你要算一个数的N次方,就要调用Pow()函数,而这个函数是定义在math.c里面的,要用这个函数,就必需调用math.h这个头文件。

头文件写法:

#include

...

//-------------------------------

#ifndef MY_POINT

#define MY_POINT

class Class1

{

}

class Class2

{

}

...

#endif

在要使用类定义的文件中加入

#include "头文件名.h "

一般来说,头文件里多数是放的函数定义或函数体。

此外,还有:

#ifndef ****

#define ****

……

#endif

之类的语句,用于控制#define 与 #endif之间的内容不被重复定义或插入。

#include 语句起的只是一个插入作用。

也就是说,#include 的文件里的内容可以随便写。

编译器使用#include 的文件里的内容来插入到#include 所在位置。

所以,你说的“头文件”没有固定格式。

如要使用其它头文件中的函数,可以直接在你的头文件中引用。

初学C语言,个人建议你使用C++Builder 6去练习和理解,当然,这要求你有一定的英语水平.在很多情况下会自动的帮你加好头文件,你可以观察它自动生成的文件,代码,以进一步学习。

example:

我截了一小段

/* math.h

Definitions for the math floating point package.

Copyright (c) 1987, 1991 by Borland International

All Rights Reserved.

*/

#ifndef __MATH_H

#define __MATH_H

#if !defined( __DEFS_H )

#include <_defs.h>

#endif

#define HUGE_VAL _huge_dble

extern double _Cdecl _huge_dble;

#define _LHUGE_VAL _huge_ldble

extern long double _Cdecl _huge_ldble;

#ifdef __cplusplus

extern "C" {

#endif

double _Cdecl acos (double __x);

double _Cdecl asin (double __x);

double _Cdecl atan (double __x);

double _Cdecl atan2 (double __y, double __x);

double _Cdecl ceil (double __x);

double _Cdecl cos (double __x);

double _Cdecl cosh (double __x);

double _Cdecl exp (double __x);

double _Cdecl fabs (double __x);

double _Cdecl __fabs__ (double __x); /* Intrinsic */

double _Cdecl floor (double __x);

double _Cdecl fmod (double __x, double __y);

double _Cdecl frexp (double __x, int *__exponent);

double _Cdecl ldexp (double __x, int __exponent);

double _Cdecl log (double __x);

double _Cdecl log10 (double __x);

double _Cdecl modf (double __x, double *__ipart);

double _Cdecl pow (double __x, double __y);

double _Cdecl sin (double __x);

double _Cdecl sinh (double __x);

double _Cdecl sqrt (double __x);

double _Cdecl tan (double __x);

double _Cdecl tanh (double __x);

long double _Cdecl acosl (long double __x);

long double _Cdecl asinl (long double __x);

long double _Cdecl atan2l (long double __x, long double __y);

long double _Cdecl atanl (long double __x);

long double _Cdecl ceill (long double __x);

long double _Cdecl coshl (long double __x);

long double _Cdecl cosl (long double __x);

long double _Cdecl expl (long double __x);

long double _Cdecl fabsl (long double __x);

long double _Cdecl floorl (long double __x);

long double _Cdecl fmodl (long double __x, long double __y);

long double _Cdecl frexpl (long double __x, int *__exponent);

long double _Cdecl ldexpl (long double __x, int __exponent);

long double _Cdecl log10l (long double __x);

long double _Cdecl logl (long double __x);

long double _Cdecl modfl (long double __x, long double *__ipart);

long double _Cdecl powl (long double __x, long double __y);

long double _Cdecl sinhl (long double __x);

long double _Cdecl sinl (long double __x);

long double _Cdecl sqrtl (long double __x);

long double _Cdecl tanhl (long double __x);

long double _Cdecl tanl (long double __x);

typedef enum

{

DOMAIN = 1, /* argument domain error -- log (-1) */

SING, /* argument singularity -- pow (0,-2)) */

OVERFLOW, /* overflow range error -- exp (1000) */

UNDERFLOW, /* underflow range error -- exp (-1000) */

TLOSS, /* total loss of significance -- sin(10e70) */

PLOSS, /* partial loss of signif. -- not used */

STACKFAULT /* floating point unit stack overflow */

} _mexcep;

#ifdef __cplusplus

}

#endif

1)所有C++的源文件均必须包含一个规范的文件头,文件头包含了该文件的名称、功能概述、作者、版权和版本历史信息等内容。

/*! @file

********************************************************************************

  

模块名 : <文件所属的模块名称>

文件名 : <文件名>

相关文件 : <与此文件相关的其它文件>

文件实现功能 : <描述该文件实现的主要功能>

作者 : <作者部门和姓名>

版本 : <当前版本号>

--------------------------------------------------------------------------------

备注 : <其它说明>

--------------------------------------------------------------------------------

修改记录 :

日 期 版本 修改人 修改内容

YYYY/MM/DD X.Y <作者或修改者名> <修改内容>

*******************************************************************************/

像这样具体的格式,最好是例子,标准的(每行前具体要空几格等)。

(2)对于是自定义的头文件:

首先要写一个头文件出来,比如说"hello.h",内容如下:

#ifndef _hello_h_

#define _hello_h_

#include

using namespace std; //调用基本输入输出库

#define SIZE 1024 //定义大小

#define LEN 100 //定义长度

class hello

{

public:

//以下为定义的类

};

#endif

然后是应用程序文件,比如叫"".你说的那些东西写在最前面,不用空格,规范的写法是用TAB键开头.例:

/*template for celanc terminal program //此处顶格写或另起一行加TAB

xx -- feature (ir, lighting ,audio ...) //此处两个TAB

yy -- device (gc100,pim,russound ...)

author : myth //单TAB

version : 5.3

date : 2008/9/9

*/

//还需要其他信息你自己添.百度这个框不支持TAB键,只好用注释标明了...

#include

int main()

{

}

差不多就这样


本文标签: 文件 头文件 定义 函数 调用