Header File Libraries In C
Header File Libraries In C
C Header File Libraries β Complete Guide
1. What is a Header File?
A header file in C contains:
- Function declarations (prototypes)
- Macros and constants
- Type definitions
They allow you to use pre-written code without rewriting it.
π‘ Think of it as a toolbox β it gives you ready-made tools (functions) so you donβt have to build them yourself.
2. Common C Header Files
Header File | Purpose & Common Functions |
---|---|
<stdio.h> | Standard input/output β printf() , scanf() , fopen() , fclose() |
<stdlib.h> | Memory management β malloc() , free() , exit() |
<string.h> | String handling β strlen() , strcpy() , strcmp() |
<math.h> | Math functions β sqrt() , pow() , sin() , cos() |
<ctype.h> | Character handling β toupper() , isdigit() , isalpha() |
<time.h> | Date & time β time() , clock() , difftime() |
<stdbool.h> | Boolean type β true , false |
3. Creating a Custom Header File
You can make your own header file to organize code better.
Step 1 β Create a header file (myheader.h
)
1
2
3
4
5
6
#ifndef MYHEADER_H // Prevents multiple inclusion
#define MYHEADER_H
void sayHello(); // Function prototype
#endif
Step 2 β Create the implementation file (myheader.c
)
1
2
3
4
5
6
#include <stdio.h>
#include "myheader.h" // Include your custom header
void sayHello() {
printf("Hello! I am your custom function!\n");
}
Step 3 β Create the main program (main.c
)
1
2
3
4
5
6
7
#include <stdio.h>
#include "myheader.h" // Use the custom header
int main() {
sayHello(); // Call the function
return 0;
}
Step 4 β Compile and Run
1
2
gcc main.c myheader.c -o main
./main
β Output:
1
Hello! I am your custom function!
This post is licensed under CC BY 4.0 by the author.