How to declare global variables and functions defined in a C program file with ease

  • Programming practice of C programmers:

      • For each C program file named myProgFile.c:

          • Create a corresponding header file named myProgFile.h

      • The header file myProgFile.h contains the declarations of:

          1. All the global variables defined in myProgFile.c

          2. All the functions defined in myProgFile.c

      • #Include myProgFile.h in:

          1. myProgFile.c (to verify the declarations in myProgFile.h)    and

          2. Any C program file that uses a global variable or a function defined in myProgFile.c

Example using header file to declare global item in a C program file

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);
}  

  

Example using header file to declare global item in a C program file

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 );

	







  

Example using header file to declare global item in a C program file

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			







  

Example using header file to declare global item in a C program file

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 using header file to declare global item in a C program file

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

Example using header file to declare global item in a C program file

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);

} 

Example using header file to declare global item in a C program file

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