请输入您要查询的百科知识:

 

词条 头文件
释义

简介

在C语言家族程序中,头文件被大量使用。一般而言,每个C++/C程序通常由头文件(header files)和定义文件(definition files)组成。头文件作为一种包含功能函数、数据接口声明的载体文件,用于保存程序的声明(declaration),而定义文件用于保存程序的实现 (implementation)。而且 .c就是你写的程序文件。

一般在一个应用开发体系中,功能的真正逻辑实现是以硬件层为基础,在驱动程序、功能层程序以及用户的应用程序中完成的。头文件的主要作用在于调用库功能,对各个被调用函数给出一个描述,其本身不包含程序的逻辑实现代码,它只起描述性作用,告诉应用程序通过相应途径寻找相应功能函数的真正逻辑实现代码。用户程序只需要按照头文件中的接口声明来调用库功能,编译器会从库中提取相应的代码。

从以上结构图来看,头文件是用户应用程序和函数库之间的桥梁和纽带。在整个软件中,头文件不是最重要的部分,但它是C语言家族中不可缺少的组成部分。做一个不算很恰当的比喻,头文件就像是一本书中的目录,读者(用户程序)通过目录,可以很方便就查阅其需要的内容(函数库)。在一本书中,目录固然重要,但绝对不是一本书的核心的、最重要的部分。

组成

C++/C程序的头文件以“.h”为后缀。以下是假设名称为 graphics.h的头文件:

#ifndefGRAPHICS_H (作用:防止graphics.h被重复引用)

#define GRAPHICS_H

#include<....> (作用:引用标准库的头文件)

...

#include"..." (作用:引用非标准库的头文件)

...

void Function1(...); (作用:全局函数声明)

...

inline();(作用:inline函数的定义)

...

class Box (作用:类结构声明)

{

...

};

#endif

从以上例子可以看出,头文件一般由三部分内容组成:(1)头文件开头处的版权和版本声明;(2)预处理块;(3)inline函数的定义;(4)函数和类结构声明等。在头文件中,用 ifndef/define/endif结构产生预处理块,用 #include 格式来引用库的头文件。头文件的这种结构,是利用C语言进行开发软件所通常具备的,属于公有知识。

示例

conio.h是一个C标准库中的头文件。

conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数等等。

包含的函数

cgets(char *);

cprintf(const char *, ...);

cputs(const char *);

cscanf(const char *, ...);

inp(unsigned short);

inpw(unsigned short);

getch(void);

getche(void);

kbhit(void);

outp(unsigned short, int);

outpw(unsigned short, unsigned short);

putch(int);

ungetch(int);

头文件种类

比如常用的还有,字符串操作的:string.h等。

参照别人的,说的挺好,看看或许对你有用。

传统 C++

#include <assert.h> //设定插入点

#include <ctype.h> //字符处理

#include <errno.h> //定义错误码

#include <float.h> //浮点数处理

#include <fstream.h> //文件输入/输出

#include <iomanip.h> //参数化输入/输出

#include <iostream.h> //数据流输入/输出

#include <limits.h> //定义各种数据类型最值常量

#include <locale.h> //定义本地化函数

#include <math.h> //定义数学函数

#include <stdio.h> //定义输入/输出函数

#include <stdlib.h> //定义杂项函数及内存分配函数

#include <string.h> //字符串处理

#include <strstrea.h> //基于数组的输入/输出

#include <time.h> //定义关于时间的函数

#include <wchar.h> //宽字符处理及输入/输出

#include <wctype.h> //宽字符分类

//////////////////////////////////////////////////////////////////////////

标准 C++

(同上的不再注释)

#include <algorithm> //STL 通用算法

#include <bitset> //STL 位集容器

#include <cctype>

#include <cerrno>

#include <clocale>

#include <cmath>

#include <complex> //复数类

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <ctime>

#include <deque> //STL 双端队列容器

#include <exception> //异常处理类

#include <fstream>

#include <functional> //STL 定义运算函数(代替运算符)

#include <limits>

#include <list> //STL 线性列表容器

#include <map> //STL 映射容器

#include <iomanip>

#include <ios> //基本输入/输出支持

#include <iosfwd> //输入/输出系统使用的前置声明

#include <iostream>

#include <istream> //基本输入流

#include <ostream> //基本输出流

#include <queue> //STL 队列容器

#include <set> //STL 集合容器

#include <sstream> //基于字符串的流

#include <stack> //STL 堆栈容器

#include <stdexcept> //标准异常类

#include <streambuf> //底层输入/输出支持

#include <string> //字符串类

#include <utility> //STL 通用模板类

#include <vector> //STL 动态数组容器

#include <cwchar>

#include <cwctype>

using namespace std;

//////////////////////////////////////////////////////////////////////////

C99 增加

#include <complex.h> //复数处理

#include <fenv.h> //浮点环境

#include <inttypes.h> //整数格式转换

#include <stdbool.h> //布尔环境

#include <stdint.h> //整型环境

#include <tgmath.h> //通用类型数学宏

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/7 16:01:26