Building programs with libraries

In the definitions of C libraries, the header of a function is specified in a form similar to that used in a function declaration. The names of parameters are not needed, however. Such definitions allow us to use functions without seeing their internal workings.

Let us look at the examples we used earlier.

No arguments
int give_three()
{
    return 3;
}
One argument
void out_one(int i)
{
    printf("%d\n",i);
}
Mixed arguments
float make_real(int i, int j, float r)
{
    return (float)(i+j) + r;
}

If we put this sequence into a file called funs.c, say, we can compile this file, to create an object file, funs.o on UNIX. Using the cc compiler on UNIX this would be done as follows

cc -c funs.c

The prototypes for these functions are:

    int give_three();
    void out_one(int);
    float make_real(int, int, float);
If we want to use functions which already exist as linkable object files, we introduce them to our program as below. The compiler assumes that they exist in a library and will be linked in before execution. stdio.h, stdlib.h etc. contain prototypes of standard functions in this form. It is often convenient to build our own libraries of functions which we can re-use in future programs.
void PrintTwo(int, int);
int Larger(int, int);
These two declarations could be placed in a header file and introduced with a #include directive. Building and using libraries is described elsewhere in these notes.


Next - Recursion.

Back to Contents page.