Header file 1
#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 ) );}
----------------------------------------------
Save the extracted part to 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);}
Program changes:
#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 ) );}
========================================================== =====
You can also select a random part, for example, extract (also called 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(){
------------------------
Program changes:
#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));}
====================================
In terms of syntax and function, both methods can be used. But the first method is better-the program is readable and error-prone.
Generally, the header file contains the function prototype, global volume declaration, and function definition.