c语言头文件怎么写~!!最好举个例子!!非常感谢!!

(1) 想让自己的程序看上去简洁,把较长的那些声明定义抽出来,放到单独的头文件里。(2)程序由多个文件组成,把各文件共用的声明定义抽出来放到单独的头文件里。 怎么定义头文件里面的内容!!

简单办法,先写完整程序,再把一部分抽出去,抽出去的存到 自己的头文件里,在抽出的地方写 #include ...

例如,完整程序(计算平均值):
#include<stdio.h>

double mean(double *y, int N){
int i;
double s=0.0;
for (i=0;i<N;i++) s=s+y[i];
s = s / (double) N;
return s;
}
void main()
{
double x[10]={1,2,3,4,5,6,7,8,9,10};
printf("mean = %lf\n", mean(x,10));
}
----------------------------------------------
抽出部分 存入 a_x.h :
double mean(double *y, int N){
int i;
double s=0.0;
for (i=0;i<N;i++) s=s+y[i];
s = s / (double) N;
return s;
}
--------------------------------
程序变:
#include<stdio.h>
#include "a_x.h"
void main()
{
double x[10]={1,2,3,4,5,6,7,8,9,10};
printf("mean = %lf\n", mean(x,10));
}
=============================================
你要是愿意随便抽一块也可以,例如抽出(也叫 a_x.h):
double mean(double *y, int N){
int i;
double s=0.0;
for (i=0;i<N;i++) s=s+y[i];
s = s / (double) N;
return s;
}
void main()
{
------------------------
程序变:
#include<stdio.h>
#include "a_x.h"
double x[10]={1,2,3,4,5,6,7,8,9,10};
printf("mean = %lf\n", mean(x,10));
}
==============================
语法上,功能上,两种抽法都可以。但第一种方法较好--程序可读性好,不易出错。

一般情况下,头文件里放 函数原型,全局量声明 和 函数定义。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-13

/*头文件内容,假设名字是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;

本回答被网友采纳
第2个回答  2018-10-26
头文件创建全部项目,总共包含三个文件:

//【01】function_01.h 代码如下:

#include <stdio.h>
void print_hello(int a, int b, int c);
void print_world(int a, int b);

//【02】function_01.cpp 代码如下:
#include "function_01.h"
void print_hello(int a, int b, int c)
{
printf("hello\n");
}

void print_world(int a, int b)
{
printf("world\n");
}

//【03】main.cpp 代码如下:
#include "function_01.h"
int main()
{
print_hello(1, 1, 1);
print_world(1, 1);

getchar();
return 0;
}


//程序运行结果为:
hello
world

第3个回答  2010-10-20
头文件随便写什么都可以
比如
h.h
#include<stdio.h>

然后你就不用在主函数里写include<stdio.h>
了,直接写#include <h.h>