🟡 Intermediate · Lesson 31
Header Files
Header Files
What are Header Files?
Header files contain function declarations, macro definitions, constants and structure declarations. They usually have a .h extension.
💡 Purpose
Header files help split a large program into smaller reusable files.
Standard Header Files
| Header | Common Use |
|---|---|
stdio.h | printf(), scanf(), file I/O |
stdlib.h | malloc(), free(), exit() |
string.h | strlen(), strcpy(), strcmp() |
math.h | sqrt(), pow(), sin() |
ctype.h | toupper(), isdigit(), isalpha() |
User-defined Header File
calc.h
#ifndef CALC_H #define CALC_H int add(int a, int b); int multiply(int a, int b); #endif
calc.c
#include "calc.h"
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}Multi-file Program
main.c
#include <stdio.h>
#include "calc.h"
int main() {
printf("Sum = %d\n", add(10, 20));
printf("Product = %d", multiply(5, 4));
return 0;
}Sum = 30
Product = 20
Compile Command
Use: gcc main.c calc.c -o app
Summary
- Header files usually contain declarations, not full program logic
- Use angle brackets for standard headers:
<stdio.h> - Use quotes for user headers:
"calc.h" - Include guards prevent duplicate declarations
- Multi-file programs become clean and reusable
Header Files क्या हैं?
Header files में function declarations, macros, constants और structures की declarations रखी जाती हैं। इनका extension सामान्यतः .h होता है।
Standard Header Files
| Header | Use |
|---|---|
stdio.h | printf(), scanf() |
stdlib.h | malloc(), free() |
string.h | strlen(), strcpy() |
math.h | sqrt(), pow() |
ctype.h | toupper(), isdigit() |
User-defined Header File
calc.h
#ifndef CALC_H #define CALC_H int add(int a, int b); int multiply(int a, int b); #endif
Multi-file Program
main.c
#include <stdio.h>
#include "calc.h"
int main() {
printf("%d", add(10, 20));
return 0;
}Compile
gcc main.c calc.c -o app
सारांश
- Header files declarations रखने के लिए होती हैं
- Standard header के लिए
< >use करें - User-defined header के लिए quotes use करें
- Include guard duplicate include रोकता है
- Large program को multiple files में divide कर सकते हैं