Skip to content

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);

#endif

2. Including Headers

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

3. Header Guards

c
#ifndef FILENAME_H
#define FILENAME_H

// header content

#endif

4. 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

Content is for learning and research only.