Function libraries

[Library-books.jpg]

All the programs in C use functions from the standard libraries of the language.  The set of functions of each library is described in an interface file, or header file. (The interface is also known as API, or application programming interface.)  The header file has the same name as the library and suffix .h:

To access a library, your program must include a copy of the corresponding interface file.  For example, if you write

#include <stdlib.h>

the preprocessor of C will add a copy of stdlib.h to your program.  (On Linux systems, the interface files are usually stored in the directory /usr/include/; but the programmer does not need to know this.)

The documentation of the functions of a library should, ideally, be in the interface file. In practice, however, the documentation is kept apart.  To see the documentation of a library function on a Linux system, type  man name-of-function  or consult the Function and Macro Index of the GNU C Library.  The documentation can also be seen in the Wikipedia C standard library entry and on the cppreference.com C Standard Library header files page.

How to write an interface for your own library

Here is a simple interface file for an imaginary library bib:

// This file: bib.h
// Brief description of the library: ...
////////////////////////////////////////

#ifndef _BIB_H
#define _BIB_H

// Inclusion of other interfaces needed
// to understand this interface.

#include <aaaa.h>
#include <bbbb.h>

// Definitions of the macros that the
// bib library uses.

#define XXX 1000

// Prototypes of the functions in the
// library.
////////////////////////////////////////

// The function xxx receives ... and
// returns ... such that ... 
//
int xxxx (int a, char c, int v[]);
 
// The function yyy receives ... and
// returns ... such that ...
//
void yyyy (double a, int b);

#endif

The file bib.h must be included in all the source files that use the bib library, as well as in the file bib.c that implements the library.

(This section was adapted from pages 275 and 286 of the book Art and Science of C by Eric Roberts.)