C Header Files

Advanced
6 min read
Updated: Nov 2026
1 example

What Is Header Files?

A header file shares declarations between source files: function prototypes, type definitions (struct, enum, typedef), macros, and extern declarations. It should not define ordinary functions or variables. Use include guards, include only what you need, and prefer static inline for tiny shared helpers.

Remember
#ifndef RECT_H
#define RECT_H
struct Rect { int w, h; };               /* type          */
int area(const struct Rect *r);          /* prototype     */
#define RECT_UNIT "cm"                   /* macro         */
extern int rect_count;                   /* extern decl   */
#endif

Cheat Sheet

Keep these facts handy while you work with Header Files in C.

TopicDetail
BelongsPrototypes, types, macros, extern declarations
Does not belongFunction bodies, variable definitions
AlwaysInclude guards
Include orderOwn header first, then system headers

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Compilation and Linking.

Example 1 — A header with a struct and inline helpers

Save as rect.h, then include it from main.c.

C
/* rect.h */
#ifndef RECT_H
#define RECT_H
struct Rect { int w, h; };
static inline int rect_area(struct Rect r)      { return r.w * r.h; }
static inline int rect_perimeter(struct Rect r) { return 2 * (r.w + r.h); }
#endif

/* main.c */
#include <stdio.h>
#include "rect.h"
int main(void) {
    struct Rect r = {4, 3};
    printf("area = %d, perimeter = %d\n", rect_area(r), rect_perimeter(r));
    return 0;
}

How It Works

  1. The header defines a type and two tiny static inline helpers.
  2. Each file that includes it gets its own copy, so there is no linker conflict.
  3. 4 * 3 = 12 and 2 * (4 + 3) = 14.

Edge Cases & Pitfalls

Check these before relying on Header Files in real programs.

def

Defining variables in headers

int count = 0; in a header creates one definition per including file and fails at link time. Use extern in the header and define once in a .c file.

over

Including too much

Large headers slow builds and hide dependencies. Include only what you use.

Key Takeaways

  • Declare in headers.
  • Define in source files.
  • Guard every header.

One line: A header file shares declarations between source files: function prototypes, type definitions (struct, enum, typedef), macros, and extern declarations.

Frequently Asked Questions

Function prototypes, type definitions, macros, and extern declarations: everything callers need to use the module.
A header declares the interface; a source file implements it. Headers are included; source files are compiled.
Continue with "C Compilation and Linking" next. Follow the pipeline from source to executable: preprocess, compile, assemble, link. The C Advanced Concepts hub lists every topic in order.

Next: Compilation and Linking

Follow the pipeline from source to executable: preprocess, compile, assemble, link.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful