next up previous
Next: Command line parameters Up: Scientific programming in C Previous: Character strings

Multi-file programs

In a program consisting of many different functions, it is often convenient to place each function in an individual file, and then use the make utility to compile each file separately and link them together to produce an executable.

There are a few common-sense rules associated with multi-file programs. Since a given file is initially compiled separately from the rest of the program, all symbolic constants which appear in that file must be defined at its start. Likewise, all referenced library functions must be accompanied by the appropriate references to header files. Also, any referenced user-defined functions must have their prototypes at the start of the file. Finally, all global variables used in the file must be declared at its start. This usually means that definitions for common symbolic constants, header files for common library functions, prototypes for common user-defined functions, and declarations for common global variables will appear in multiple files. Note that a given global variable can only be initialized in one of its declaration statements, which is regarded as the true declaration of that variable [conventionally, the true declaration appears in the file containing the function main()]. Indeed, the other declarations, which we shall term definitions, must be preceded by the keyword extern to distinguish them from the true declaration.

As an example, let us take the program printfact4.c, listed previously, and break it up into multiple files, each containing a single function. The files in question are called main.c and factorial.c. The listings of the two files which make up the program are as follows:

/* main.c */
/*
  Program to print factorials of all integers
  between 0 and 20
*/

#include <stdio.h>

/* Prototype for fucntion factorial() */
void factorial();    

/* Global variable declarations */
int j;               
double fact;

int main() 
{
  /* Print factorials of all integers between 0 and 20 */
  for (j = 0; j <= 20; ++j)
    {
      factorial();
      printf("j = %3d    factorial(j) = %12.3e\n", j, fact);
    }
  return 0;
}
and
/* factorial.c */
/* 
   Function to evaluate factorial (in floating point form)
   of non-negative integer j. Result stored in variable fact.
*/

#include <stdio.h>
#include <stdlib.h>

/* Global variable definitions */
extern int j;               
extern double fact;

void factorial() 
{
  int count;

  /* Abort if j is negative integer */
  if (j < 0) 
    {
      printf("\nError: factorial of negative integer not defined\n");
      exit(1);
    }

  /* Calculate factorial */
  for (count = j, fact = 1.; count > 0; --count) fact *= (double) count;

  return;      
}
Note that all library functions and user-defined functions referenced in each file are declared (either via a header file or a function prototype) at the start of that file. Note, also, the distinction between the global variable declarations in the file main.c and the global variable definitions in the file factorial.c.


next up previous
Next: Command line parameters Up: Scientific programming in C Previous: Character strings
Richard Fitzpatrick 2006-03-29