Suite101

C Header Files

Breaking up code into separate source files, handling strings, mathematical functions and standardized input and output.

© Guy Lecky-Thompson

API Calls, SXC.hu
In this C header file tutorial,we explore the use of header files in C,ways in which they can be used, and the philosophy for breaking up code into separate source files.

Introduction

C header files are used to provide a way of telling the compiler what interface specific functions require in order to be used. The compiler has no interest in what the source code actually does, but it does need to know how to interface to that code, and the header file gives it that information.

Every external function will be mentioned in a header file, including libraries that are pre-compiled into object code, and source files required to build the C program. We will look at the standard header files, user defined header files, and how they fit into the compiling and linking sequence.

Standard Header Files

By now, the reader should be aware that there are many operations that are not catered for in the C programming language; string manipulation, for example. However, the creators of all compiler sets will make available a set of libraries to provide this 'missing' functionality.

These are collectively known as the standard libraries and include:

  • string.h : for string handling
  • stdlib.h : for some miscellaneous functions
  • stdio.h : standardized input and output
  • math.h : mathematical functions

We place references to these at the start of the source code file that uses one or more functions from that library. So, a simple hello world application might require the formatted output offered in the stdio.h header file:

#include // Formatted I/O
void main ( void ) // Entry point function
{
printf("Hello World"); // Output
}

This program simply writes Hello World to the screen. What is important to note is that the code for the function printf is not defined in the program itself, but in a library that comes pre-compiled. The compiler can compile the source code above, thanks to the #include line, which tells it where the definition of printf can be found.

User Defined Header Files

If we suppose that the programmer has a function that provides additional processing capabilities, then they might want to create a separate source code file to contain that function. Perhaps only one function within a large source code file comprising many functions should be exported, and keeping it separate from the main source code makes programming sense.

In these situations, it is required to provide a user defined header file so that the compiler knows how to call this function. Our original code sample might then look like this:

#include "MyOutput.h" // My output functions
void main ( void ) // Entry point function
{
PrintNicely ("Hello World"); // Output
}

In this sample, we have referred to a user defined header file (note the quotes around the name) MyOutput.h. The quotes are required to tell the compiler not to look along the standard library path, but to look in the same path as the source file.

Somewhere along the line, we will also need to create a MyOutput.c file to contain the implementation of PrintNicely, otherwise the computer will not know what to do when it calls the function. This code might look like:

#include // Formatted I/O
#include "MyOutput.h" // My output function
void PrintNicely (char * string)
{
printf( string ); // Output
}

(Note that we also need to #include the MyOutput.h file as well as any other header files that provide additional processing)

All that this function does is call the standard output function, as before, but it could do other processing instead of just calling printf. And, for completeness, the MyOutput.h file:

void PrintNicely (char * string);

All this does is provide a globally accessible definition of PrintNicely. This is also known as the function prototype.

Compiling and Linking

Finally, a word about compiling and linking header files and the associated source code. In the makefile, the programmer will specify the source code that needs to be compiled - file by file. The compiler just compiles these into object code. As long as the C program is syntactically correct, the object code will be created.

It is the linker that will combine the object code, library files, and create the executable file. The library file names may differ from the source code file names that they have been created from. Object code will have the same name as the source code file that it was compiled from.

The header files are what links the definition of the function with the implementation. If a programmer needs to use a library, they need a header file. If they know what is in the library, they can create it themselves, but usually, all libraries will come with suitable header files. These are usually stored in the compilers /include path.


The copyright of the article C Header Files in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish C Header Files in print or online must be granted by the author in writing.



Comments
May 14, 2008 4:58 AM
Guest :
here explain standrad comments very good.
want know about graphics.h
Sep 7, 2008 8:40 PM
Guest :
nice example
2 Comments


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo