C Header Files
Header files contain declarations and function prototypes.
1. Creating Header Files
c
// myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H
int add(int a, int b);
void print_message(const char *msg);
#endif2. Including Headers
c
#include <stdio.h> // system header
#include "myheader.h" // user header3. Header Guards
c
#ifndef FILENAME_H
#define FILENAME_H
// header content
#endif4. Function Prototypes
c
// In header file
int calculate(int x, int y);
// In source file
int calculate(int x, int y) {
return x + y;
}5. Best Practices
- Use header guards to prevent multiple inclusion
- Put only declarations in headers
- Put definitions in source files
- Use meaningful names