当前位置:文档之家› C语言头文件作用及写法

C语言头文件作用及写法


如要使用其它头文件中的函数,可以直接在你的头文件中引用。
初学 C 语言,个人建议你使用 C++Builder 6 去练习和理解,当然,这要求你有一定的英语 水平.在很多情况下会自动的帮你加好头文件,你可以观察它自动生成的文件,代码,以进 一步学习。 example: 我截了一小段 /* math.h
#ifndef _hello_h_ #define _hello_h_
#include <iostream> using namespace std; //调用基本输入输出库
#define SIZE 1024 //定义大小 #define LEN 100 //定义长度
class hello { public: //以下为定义的类 };
double double double double double double double double double double
_Cdecl ldexp (double __x, int __exponent); _Cdecl log (double __x); _Cdecl log10 (double __x); _Cdecl modf (double __x, double *__ipart); _Cdecl pow (double __x, double __y); _Cdecl sin (double __x); _Cdecl sinh (double __x); _Cdecl sqrt (double __x); _Cdecl tan (double __x); _Cdecl tanh (double __x);
}
class Class2 { } ... #endif 在要使用类定义的文件中加入 #include "头文件名.h " 一般来说,头文件里多数是放的函数定义或函数体。 此外,还有: #ifndef **** #define **** …… #endif
之类的语句,用于控制#define 与 #endif 之间的内容不被重复定义或插入。 #include 语句起的只是一个插入作用。 也就是说,#include 的文件里的内容可以随便写。 编译器使用#include 的文件里的内容来插入到#include 所在位置。 所以,你说的“头文件”没有固定格式。
UNDERFLOW, /* underflow range error -- exp (-1000) */
TLOSS,
/* total loss of significance -- sin(10e70) */
PLOSS,
/* partial loss of signif. -- not used */
#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);
#endif
然后是应用程序文件,比如叫"hello.cpp".你说的那些东西写在最前面,不用空格,规范的写法 是用 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了... #include <hello.h> int main() { } 差不多就这样
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);
STACKFAULT /* floating point unit stack overflow
*/
} _mexcep;
#ifdef __cplusplus } #endif 1)所有 C++的源文件均必须包含一个规范的文件头,文件头包含了该文件的名称、功能概 述、Байду номын сангаас者、版权和版本历史信息等内容。 /*! @file ************************************************************************ ******** <PRE> 模块名 : <文件所属的模块名称> 文件名 : <文件名> 相关文件 : <与此文件相关的其它文件> 文件实现功能 : <描述该文件实现的主要功能> 作者 : <作者部门和姓名> 版本 : <当前版本号> -------------------------------------------------------------------------------备注 : <其它说明> -------------------------------------------------------------------------------修改记录 : 日 期 版本 修改人 修改内容 YYYY/MM/DD X.Y <作者或修改者名> <修改内容> </PRE> ************************************************************************ *******/ 像这样具体的格式,最好是例子,标准的(每行前具体要空几格等)。 (2)对于是自定义的头文件: 首先要写一个头文件出来,比如说"hello.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;
C 语言头文件作用及写法
头文件几个好处:
1,头文件可以定义所用的函数列表,方便查阅你可以调用的函数; 2,头文件可以定义很多宏定义,就是一些全局静态变量的定义,在这样的情况下,只要修 改头文件的内容,程序就可以做相应的修改,不用亲自跑到繁琐的代码内去搜索。 3,头文件只是声明,不占内存空间,要知道其执行过程,要看你头文件所申明的函数是在 哪个.c 文件里定义的,才知道。 4,他并不是 C 自带的,可以不用。 5,调用了头文件,就等于赋予了调用某些函数的权限,如果你要算一个数的 N 次方,就要 调用 Pow()函数,而这个函数是定义在 math.c 里面的,要用这个函数,就必需调用 math.h 这个头文件。 头文件写法: #include <vcl.h> ... //------------------------------#ifndef MY_POINT #define MY_POINT class Class1 {
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);
typedef enum
{
DOMAIN = 1, /* argument domain error -- log (-1)
*/
SING,
/* argument singularity -- pow (0,-2)) */
OVERFLOW, /* overflow range error -- exp (1000) */
Definitions for the math floating point package.
Copyright (c) 1987, 1991 by Borland International All Rights Reserved. */
#ifndef __MATH_H #define __MATH_H
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);
相关主题