C Basics · beginner · ~10 min

Header files

Split code across .c and .h files cleanly.

Lesson

A .h header declares functions, types, and macros. A .c source file defines them. #include "foo.h" literally pastes the header into the source at that point.

Use include guards (#ifndef FOO_H / #define FOO_H / #endif) or #pragma once to prevent double-inclusion when headers include other headers. Keep headers as small as possible — they're effectively your library's public interface.

Code examples

// math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
int sum(int a, int b);
#endif

// math_utils.c
#include "math_utils.h"
int sum(int a, int b) { return a + b; }

Common mistakes

  • Defining functions in a header — the definition gets pasted into every including file → linker errors. Headers declare; .c files define.
  • Forgetting include guards on a heavily-included header leads to duplicate-definition errors.