|
Suppose we have a global variable someGlobalVar and 3 functions defined in program file func3.c:
func3.c | func3.h |
---|---|
double someGlobalVar = 4; int f( float x ) { return(x*x); } float g( int x ) { return(x*x*x); } float h( float x ) { return(x*x*x*x); } |
|
We first create a header file containing the declarations of all the global variables and all the functions:
func3.c | func3.h |
---|---|
double someGlobalVar = 4; int f( float x ) { return(x*x); } float g( int x ) { return(x*x*x); } float h( float x ) { return(x*x*x*x); } |
extern double someGlobalVar; int f( float x ); float g( int x ); float h( float x ); |
It is a good practice to avoid a recursive include chain by using the ifndef "trick" (see )
func3.c | func3.h |
---|---|
double someGlobalVar = 4; int f( float x ) { return(x*x); } float g( int x ) { return(x*x*x); } float h( float x ) { return(x*x*x*x); } |
#ifndef func3_h #define func3_h extern double someGlobalVar; int f( float x ); float g( int x ); float h( float x ); #endif |
Always #include the header file in the source C program file:
func3.c | func3.h |
---|---|
#include "func3.h"
double someGlobalVar = 4;
int f( float x )
{
return(x*x);
}
float g( int x )
{
return(x*x*x);
}
float h( float x )
{
return(x*x*x*x);
}
|
#ifndef func3_h #define func3_h extern double someGlobalVar; int f( float x ); float g( int x ); float h( float x ); #endif |
Reason: this practice allows the C compiler to verify that the declarations and the definitions are consistent
Example declation verification: suppose we change the function definition of f(float x) to f(double x):
func3.c | func3.h |
---|---|
#include "func3.h" double someGlobalVar = 4; int f( double x ) { return(x*x); } float g( int x ) { return(x*x*x); } float h( float x ) { return(x*x*x*x); } |
#ifndef func3_h #define func3_h extern double someGlobalVar; int f( float x ); // Wrong !! float g( int x ); float h( float x ); #endif |
The C compiler will detect the error when you re-compile func3.c
DEMO: demo/C/set4/func3.c
Suppose a C program file main3.c uses some global variable or function defined in func3.c:
func3.c | main3.c |
---|---|
#include "func3.h" double someGlobalVar = 4; int f( double x ) { return(x*x); } float g( int x ) { return(x*x*x); } float h( float x ) { return(x*x*x*x); } |
#include <stdio.h> int main( int argc, char *argv[]) { short a = 2; int b; printf("someGlobalVar = %f\n", someGlobalVar); b = f( a ); printf("f(%d) = %d\n", a, b); b = g( a ); printf("g(%d) = %d\n", a, b); } |
We can easily declare all global variables and functions defined in func3.c using an #include directive:
func3.c | main3.c |
---|---|
#include "func3.h" double someGlobalVar = 4; int f( double x ) { return(x*x); } float g( int x ) { return(x*x*x); } float h( float x ) { return(x*x*x*x); } |
#include <stdio.h>
#include "func3.h"
int main( int argc, char *argv[])
{
short a = 2;
int b;
printf("someGlobalVar = %f\n",
someGlobalVar);
b = f( a );
printf("f(%d) = %d\n", a, b);
b = g( a );
printf("g(%d) = %d\n", a, b);
}
|
DEMO: demo/C/set4/main3.c + func3.c