Separate compilation
Let's look at separate compilation in detail. We can break a
large program that calls many functions into three pieces, one source
file that contains main(), one source file that contains the function
definitions and a header file that contains the function prototypes and
any symbolic constants.
The source code pieces will be compiled separately to make their own object
code files. The object files can then be combined by the linker along with
other necessary object files to make the executable file.
The monolithic program
/********************************************
* File: big.c
* Author: S. Bogar
* Date: 8/3/99
* Section: 101
* SSN: 123-45-6789
* EMail: bogar@cs.umbc.edu
*
* This file uses lots and lots of functions.
*********************************************/
#include
/* Prototypes for the functions we use. */
int Sum (int x, int y) ;
int Max (int x, int y) ;
int Min (int x, int y) ;
double Avg (int x, int y) ;
int Dist (int x, int y) ;
int main ()
{
int a, b ;
/* get two ints from the user */
printf("Enter first number: ") ;
scanf ("%d", &a) ;
printf("Enter second number: ") ;
scanf ("%d", &b) ;
printf("\n") ;
/* use functions for perform simple arithmetic.
** print the results of each function call */
printf("The sum of %d and %d is: %d\n",
a, b, Sum (a, b) ) ;
printf("The smaller of %d and %d is: %d\n",
a, b, Min (a, b) ) ;
printf("The larger of %d and %d is: %d\n",
a, b, Max (a, b) ) ;
printf("The average of %d and %d is: %f\n",
a, b, Avg (a, b) ) ;
printf("The distance between %d and %d is: %d\n",
a, b, Dist (a, b) ) ;
/* we're done!! */
printf("That's all folks.\n") ;
return 0;
}
/********************************************
* Function: Sum
* Usage: z = Sum (x, y)
*
* Input: two integers to be added
* Output: Returns the sum of x and y.
*********************************************/
int Sum (int x, int y)
{
int sum ;
sum = x + y ;
return sum ;
}
/*********************************************
* Function: Max
* Usage: z = Max (x, y)
*
* Inputs: two integers to be compared
* Output: Returns the larger of x and y.
*********************************************/
int Max (int x, int y)
{
int max ;
if (x > y) {
max = x ;
} else {
max = y ;
}
return max ;
}
/*********************************************
* Function: Min
* Usage: z = Min (x, y)
*
* Inputs: two integers to be compared
* Output: Returns the smaller of x and y.
*********************************************/
int Min (int x, int y)
{
int min ;
if (x < y) {
min = x ;
} else {
min = y ;
}
return min ;
}
/*********************************************
* Function: Avg
* Usage: z = Avg (x, y)
*
* Inputs: two integers to be compared
* Output: Returns average of x and y as a *** double ***
*
*********************************************/
double Avg (int x, int y)
{
double average ;
average = (x + y) / 2.0 ;
return average ;
}
/*********************************************
* Function: Dist
* Usage: z = Dist (x, y)
*
* Input: two integers for which distance is calulated
* Output:Returns the absolute value of x - y.
*********************************************/
int Dist (int x, int y)
{
int distance ;
distance = x - y ;
if (distance < 0) {
distance = -distance ;
}
return distance ;
}
The Sample Run
linux2[75] % gcc -Wall -ansi big.c
linux2[76] % a.out
Enter first number: 2
Enter second number: 5
The sum of 2 and 5 is: 7
The smaller of 2 and 5 is: 2
The larger of 2 and 5 is: 5
The average of 2 and 5 is: 3.500000
The distance between 2 and 5 is: 3
That's all folks.
linux2[77] % !a
a.out
Enter first number: 5
Enter second number: 2
The sum of 5 and 2 is: 7
The smaller of 5 and 2 is: 2
The larger of 5 and 2 is: 5
The average of 5 and 2 is: 3.500000
The distance between 5 and 2 is: 3
That's all folks.
linux2[78] % !a
a.out
Enter first number: 113
Enter second number: 49135
The sum of 113 and 49135 is: 49248
The smaller of 113 and 49135 is: 113
The larger of 113 and 49135 is: 49135
The average of 113 and 49135 is: 24624.000000
The distance between 113 and 49135 is: 49022
That's all folks.
linux2[79] %
Last Modified -