C Header Files

Header files contain declarations and function prototypes.

1. Creating Header Files

// myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H

int add(int a, int b);
void print_message(const char *msg);

#endif

2. Including Headers

#include <stdio.h>     // system header
#include "myheader.h"  // user header

3. Header Guards

#ifndef FILENAME_H
#define FILENAME_H

// header content

#endif

4. Function Prototypes

// 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