Using Multiple files


File 1 - main.c


/***************************************************************
*                                                              *
*  Author: Brenda Sonderegger                                  *
*  Last modified: Sept. 21, 1999                               *
*  Purpose: provide an example of using multiple files within  *
*           a project.                                         *
*  Functions: main()                                           *
*             others that are specific to this application     *
*                                                              *
*  Functions called: get_int() in "myio_c.h"                   *
*                                                              *
***************************************************************/
 
#include <stdio.h>
#include "myio_c.h"

int main(void)
{
  int i = 0;

  while (i != 5)
  {
    printf("\n5 to quit ");
    get_int(&i);
  }

  return (0);
}


File 2 - header file named myio_c.h


#ifndef _MYIO_C_H
#define _MYIO_C_H

#include <stdio.h>
#include <ctype.h>

/*****************************************************
*                                                    *
* File Name: myio_c.h                                *
* Author: Brenda Sonderegger (all functions)         *
* Contents:                                          *
*    get_int()                                       *
*    get_int_low()                                   *
*    get_int_bound()                                 *
*                                                    *
*****************************************************/  

  /*****************************************************
  * void get_int(int *value)                           *
  * Author: Brenda Sonderegger                         *
  * Last Modified: Sept. 22, 1998                      *
  * Parameters: single address                         * 
  * Return value: none                                 *
  * Side Effect: puts an integer value into the memory *
  *              location specified by the address and *
  *              if an error occurs, the input stream  *
  *              is emptied                            *
  * Description: guarantees that the input value is an *
  *              integer.  Inputs of a, 23c, 2.4 are   *
  *              all considered to be illegal          *
  *****************************************************/
void get_int(int *value);



  /*****************************************************
  * void get_int_low(int *value, int low)              *
  * Author: Brenda Sonderegger                         *
  * Last Modified: Sept. 16, 1998                      *
  * Parameters: single address, and single integer     * 
  * Return value: none                                 *
  * Side Effect: puts an integer value into the memory *
  *              location specified by the address that*
  *              if guaranteed to be greater than or   *
  *              equal to the low value                *
  * Description: guarantees that the input value is an *
  *              integer greater than or equal to low  *
  * Functions called: get_int()                        *
  *****************************************************/
void get_int_low(int *value, int low);



  /*****************************************************
  * void get_int_bound(int *value, int low, int high)  *
  * Author: Brenda Sonderegger                         *
  * Last Modified: Sept. 16, 1998                      *
  * Parameters: single address, and two integers       * 
  * Return value: none                                 *
  * Side Effect: puts an integer value into the memory *
  *              location specified by the address that*
  *              if guaranteed to be greater than or   *
  *              equal to the low value and less than  *
  *              or equal to the high value            *
  * Description: guarantees that the input value is an * 
  *              integer greater than or equal to low  *
  *              and less than or equal to high        *
  * Functions called: get_int()                        *
  *****************************************************/
void get_int_bound(int *value, int low, int high);
 
#endif


File 3 - source code for all prototypes in myio_c.c


#include "myio_c.h"

/*****************************************************
*                                                    *
* File Name: myio_c.c                                *
* Author: Brenda Sonderegger (all functions)         *
* Contents:                                          *
*    get_int()                                       *
*    get_int_low()                                   *
*    get_int_bound()                                 *
*                                                    *
*****************************************************/  

  /*****************************************************
  * void get_int(int *value)                           *
  * Author: Brenda Sonderegger                         *
  * Last Modified: Sept. 22, 1998                      *
  * Parameters: single address                         * 
  * Return value: none                                 *
  * Side Effect: puts an integer value into the memory *
  *              location specified by the address and *
  *              if an error occurs, the input stream  *
  *              is emptied                            *
  * Description: guarantees that the input value is an *
  *              integer.  Inputs of a, 23c, 2.4 are   *
  *              all considered to be illegal          *
  *****************************************************/
void get_int(int *value)
{
 
   scanf("%d", value);
   /** the robust version should be substituted for this **/

}



  /*****************************************************
  * void get_int_low(int *value, int low)              *
  * Author: Brenda Sonderegger                         *
  * Last Modified: Sept. 16, 1998                      *
  * Parameters: single address, and single integer     * 
  * Return value: none                                 *
  * Side Effect: puts an integer value into the memory *
  *              location specified by the address that*
  *              if guaranteed to be greater than or   *
  *              equal to the low value                *
  * Description: guarantees that the input value is an *
  *              integer greater than or equal to low  *
  * Functions called: get_int()                        *
  *****************************************************/
void get_int_low(int *value, int low)
{
   do
   {
     get_int(value);
     if (*value < low)
       printf("\nValue must be >= %d.  Try again.\n", low);
   }
   while (*value < low);
}


  /*****************************************************
  * void get_int_bound(int *value, int low, int high)  *
  * Author: Brenda Sonderegger                         *
  * Last Modified: Sept. 16, 1998                      *
  * Parameters: single address, and two integers       * 
  * Return value: none                                 *
  * Side Effect: puts an integer value into the memory *
  *              location specified by the address that*
  *              if guaranteed to be greater than or   *
  *              equal to the low value and less than  *
  *              or equal to the high value            *
  * Description: guarantees that the input value is an *
  *              integer greater than or equal to low  *
  *              and less than or equal to high        *
  * Functions called: get_int()                        *
  *****************************************************/
void get_int_bound(int *value, int low, int high)
{
   do
   {
     get_int(value);
     if ((*value < low) || (*value > high))
       printf("\nValue must be between %d and %d, inclusive.  Try again.\n", 
                 low, high);
   }
   while ((*value < low) || (*value > high));
} 
 

Compiling multiple files

How you compile a program consisting of multiple user-defined files depends on your environment.