A header file in C is a file with a .h
extension that typically contains function declarations, macro definitions, and type definitions. Its purpose is to share this common information across multiple source files (.c
files) in a project.
Think of a header
file as a table of contents for a library of code. It tells the compiler
what functions are available to use, but the actual implementation (the code
that makes the functions work) is stored elsewhere, usually in a corresponding .c
source file or a pre-compiled library.
The #include
Preprocessor Directive
You use a header
file in your program with the #include
preprocessor directive. This directive tells the C
preprocessor to find the specified file and essentially copy and paste its
entire content into the current source file before the actual compilation
begins.
There are two ways
to use #include
:
1.  #include
<filename.h>
(using
angle brackets)
o   This is used for standard library header files.
o   It tells the preprocessor to look for the file in the standard system directories where all the standard library files are stored.
o   Example: #include
<stdio.h>
2.  #include
"filename.h"
(using
double quotes)
o   This is used for your own custom header files that you create as part of your project.
o   It tells the preprocessor to look for the file in the current directory first. If it's not found there, it will then search the standard system directories.
o   Example: #include
"my_functions.h"
Common Standard Library Header Files
C comes with a rich set of standard libraries, each with a corresponding header file that provides declarations for useful functions.
Header File |
Purpose |
Common Functions / Macros |
|
Standard Input/Output |
|
|
Standard Library Utilities |
|
|
String Manipulation |
|
|
Common Mathematical Functions |
|
|
Character Type Functions |
|
Â
Example: Creating and Using a Custom Header File
Creating your own header files is essential for organizing larger projects. Here’s how you do it in three steps.
Step 1: Create
the Header File (mymath.h
)
This file contains
the function prototype (declaration). We also add include guards
(#ifndef
, #define
, #endif
) to prevent errors if the header is included more
than once in the same file.
C
// mymath.h
Â
#ifndef MYMATH_HÂ // If MYMATH_H has not been defined...
#define MYMATH_HÂ // ...then define it.
Â
// Function prototype for adding two integers
int add(int a, int b);
Â
#endif // End of the include guard block
Step
2: Create the Implementation File (mymath.c
)
This source file contains the actual function definition (the code that makes the function work).
C
// mymath.c
Â
#include "mymath.h" // Include the header to link the declaration
Â
// Function definition
int add(int a, int b) {
  Â
return a + b;
}
Step
3: Use the Function in Your Main Program (main.c
)
Now, you can
include your custom header in your main program and use the add
function as if it were a standard library function.
C
// main.c
Â
#include <stdio.h>
#include "mymath.h" // Include your custom header file
Â
int main() {
  Â
int x =
10;
  Â
int y =
20;
  Â
int sum;
Â
  Â
// Call the function from your library
   sum = add(x, y);
Â
  Â
printf(
"The sum of %d and %d is: %d\n", x, y, sum);
Â
  Â
return
0;
}
To
compile this project, you would need
to compile both .c
files together: gcc main.c mymath.c -o myprogram
Â
Â