C Programming Tutorial

Go to the Table of Contents

Vist the Gifcom Corporation

Plug-in C expansions. Header files.

The core of the C language is small and simple. Special functionality is provided in the form of libraries of ready-made functions. This is what makes C so portable. Some libraries are provided for you, giving you access to many special abilities without needing to reinvent the wheel. You can also make your own, but to do so you need to know how your operating system builds libraries. We shall return to this later.

Libraries are files of ready-compiled code which we can merge with a C program at compilation time. Each library comes with a number of associated header files which make the functions easier to use. For example, there are libraries of mathematical functions, string handling functions and input/output functions and graphics libraries. It is up to every programmer to make sure that libraries are added at compilation time by typing an optional string to the compiler. For example, to merge with the math library libm.a you would type

 cc -o program_name prog.c -lm

when you compile the program. The -lm means: add in libm. If we wanted to add in the socket library libsocket.a to do some network programming as well, we would type

 cc -o program_name prog.c -lm -lsocket

and so on.

Why are these libraries not just included automatically? Because it would be a waste for the compiler to add on lots of code for maths functions, say, if they weren't needed. When library functions are used in programs, the appropriate library code is included by the compiler, making the resulting object code often much longer.

Libraries are supplemented by header files which define macros, data types and external data to be used in conjunction with the libraries. Once a header file has been included, it has effectively added to the list of reserved words and commands in the language. You cannot then use the names of functions or macros which have already been defined in libraries or header files to mean anything other than what the library specifies.

The most commonly used header file is the standard input/output library which is called stdio.h. This belongs to a subset of the standard C library which deals with file handling. The math.h header file belongs to the mathematics library libm.a. Header files for libraries are included by adding to the source code:

#include header.h

at the top of a program file. For instance:

#include "myheader.h"

includes a personal header file which is in the current directory. Or

#include <stdio.h>

includes a file which lies in a standard directory like /usr/include.

The #include directive is actually a command to the C preprocessor, which is dealt with more fully later, See Preprocessor.

Some functions can be used without having to include library files or special libraries explicitly since every program is always merged with the standard C library, which is called libc.

#include <stdio.h>

main ()

printf ("C standard I/O file is included\n");
printf ("Hello world!");

A program wishing to use a mathematical function such as cos would need to include a mathematics library header file.

#include <stdio.h>
#include <math.h>

main ()

{ double x,y;

y = sin (x);
printf ("Maths library ready");

A particular operating system might require its own special library for certain operations such as using a mouse or for opening windows in a GUI environment, for example. These details will be found in the local manual for a particular C compiler or operating system.

Although there is no limit, in principle, to the number of libraries which can be included in a program, there may be a practical limit: namely memory, since every library adds to the size of both source and object code. Libraries also add to the time it takes to compile a program. Some operating systems are smarter than others when running programs and can load in only what they need of the large libraries. Others have to load in everything before they can run a program at all, so many libraries would slow them down.

To know what names libraries have in a particular operating system you have to search through its documentation. Unix users are lucky in having an online manual which is better than most written ones.