c中头文件怎么写( 二 )


例如,完整程序(计算平均值):
#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));
}
==============================
语法上,功能上,两种抽法都可以 。但第一种方法较好--程序可读性好,不易出错 。
一般情况下,头文件里放 函数原型,全局量声明 和 函数定义 。
5.C语言如何写头文件/*头文件内容,假设名字是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文件,会出现重复定义问题
6.c语言头文件怎么写呀一个.h文件要 对应有一个.c文件,这样写助于查看和修改程序 。比如a.h 和 a.c;:
在.h文件中
#ifndef __A_H_
#define __A_H_
#include
void trans2(double B) ;
double trans1() ;
#endif
在.c文件中
#include "a.h"
把相应的函数定义写在.c文件中
再在main.c文件中
#include "a.h"
#include
main()
{
double trans1() //由度分秒到弧度
{
double B1,B11,B12,B13,B111;
scanf("%lf°%lf′%lf″",&B11,&B12,&B13);
B111=fabs(B11); //B11可能为负值
B1=B111+B12/60.0+B13/3600.0;
B1=B1*atan(1)/45.0;
if(B11B1=-B1;
return B1;
}
void trans2(double B) //由弧度到度分秒并输出角度值
{
int a,b;
double B0;
B0=fabs(B); //B可能为负值
double c;
B0=B0*45.0/atan(1);
a=int(B0);
b=int((B0-a)*60);
c=(B0-a)*3600-b*60;
if((int)(c)==60) //为了避免出现59′60″这种情况,不过好像不起作用,不知道为什么,原来是int没有加括号
{
b=b+1;
c=0.0;
}
if(b==60)
{
b=0;
a=a+1;
}
if(Ba=-a;
printf("%d°%d′%.4f″\n",a,b,c);
}
}
就可以在main.c函数中调用a.c中的函数了
很好理解吧 希望对你有帮助
请采纳答案,支持我一下 。

c中头文件怎么写

文章插图