Header Files in C
Deep dive into header files! Learn to create your own .h files, understand include guards, organize declarations, and follow best practices for modular code.
Track Your Progress
Sign in to save your learning progress
What You Will Learn
- ✓Understand the purpose of header files
- ✓Create your own header files
- ✓Use include guards to prevent double inclusion
- ✓Know what goes in .h vs .c files
- ✓Follow header file best practices
01What are Header Files?
Header files (with .h extension) contain declarations that are shared between multiple source files. They are the foundation of modular C programming.
What Goes in Header Files?
Include in .h
- * Function prototypes
- * Type definitions (struct, enum, typedef)
- * Macro definitions (#define)
- * extern variable declarations
- * Include guards
Keep in .c
- * Function definitions (body)
- * Variable definitions
- * static functions
- * The main() function
02Creating Your Own Header Files
Let's create a simple math library with a header file:
1#ifndef MYMATH_H // Include guard - prevents double inclusion2#define MYMATH_H34// Function prototypes (declarations)5int add(int a, int b);6int subtract(int a, int b);7int multiply(int a, int b);8double divide(int a, int b);910// Macro definitions11#define PI 3.1415926535912#define SQUARE(x) ((x) * (x))1314// Type definitions15typedef struct {16 double x;17 double y;18} Point;1920#endif // MYMATH_H1#include "mymath.h" // Include our header23// Function definitions (implementations)4int add(int a, int b) {5 return a + b;6}78int subtract(int a, int b) {9 return a - b;10}1112int multiply(int a, int b) {13 return a * b;14}1516double divide(int a, int b) {17 if (b == 0) {18 return 0; // Handle division by zero19 }20 return (double)a / b;21}1#include <stdio.h>2#include "mymath.h" // Include our custom header34int main() {5 printf("5 + 3 = %d\n", add(5, 3));6 printf("5 - 3 = %d\n", subtract(5, 3));7 printf("5 * 3 = %d\n", multiply(5, 3));8 printf("5 / 3 = %.2f\n", divide(5, 3));9 10 printf("PI = %.5f\n", PI);11 printf("SQUARE(5) = %d\n", SQUARE(5));12 13 Point p = {3.0, 4.0};14 printf("Point: (%.1f, %.1f)\n", p.x, p.y);15 16 return 0;17}Expected Output:
5 - 3 = 2
5 * 3 = 15
5 / 3 = 1.67
PI = 3.14159
SQUARE(5) = 25
Point: (3.0, 4.0)
03Include Guards
Include guards prevent a header file from being included multiple times, which would cause redefinition errors.
Traditional #ifndef Guard
1#ifndef MYHEADER_H2#define MYHEADER_H34// Your declarations here56#endifModern #pragma once
1#pragma once23// Your declarations here45// Simpler but not 100% portableWhy include guards matter:
Without guards, if a.h includes b.h, and main.c includes both, you get duplicate definitions = compiler errors!
04System vs User Headers
#include <header.h>
Searches system directories first (like /usr/include)
1// System/standard library headers2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include <math.h>#include "header.h"
Searches current directory first, then system directories
1// Your own headers2#include "mymath.h"3#include "utils.h"4#include "../lib/helper.h"05Best Practices
1. One header per module
Each .c file should have a corresponding .h file with its public interface.
2. Include what you use
Each file should include all headers it needs directly, not rely on transitive includes.
3. Minimize includes in headers
Use forward declarations when possible to reduce compilation time.
4. Self-contained headers
A header should compile on its own - include everything it needs.
5. Use descriptive guard names
Convention: PROJECT_FOLDER_FILENAME_H (e.g., MYAPP_UTILS_MATH_H)
06Summary
Header files enable modular programming:
- * Put declarations in
.h, definitions in.c - * Always use include guards (#ifndef or #pragma once)
- * Use angle brackets for system headers, quotes for your own
- * Keep headers minimal and self-contained
Test Your Knowledge
Related Tutorials
C Preprocessor Directives
Code that runs before compilation! Learn #include, #define macros, and conditional compilation with #ifdef.
Multi-File Programs in C
Organize large projects into multiple files! Learn about header files, source files, the extern keyword, and how to compile multi-file programs.
Debugging with LLDB
Master LLDB, the modern debugger for macOS and Linux! Learn installation, essential commands, and how to transition from GDB. Perfect for Xcode and Clang users.
Have Feedback?
Found something missing or have ideas to improve this tutorial? Let us know on GitHub!