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 integersint add(int a, int b); #endif // End of the include guard blockStep
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 definitionint 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
Â
Â