🟡 Intermediate  ·  Lesson 31

Header Files

What are header files?

A header file is a file ending in .h that holds declarations — things like function prototypes, macros and type definitions. You do not put the full working code here; you put the "announcements" that tell the compiler what exists.

You pull a header into your program with #include. When you write #include <stdio.h>, you are borrowing the declarations for printf, scanf and friends so you can use them. (See preprocessor directives for how #include works.)

Standard header files

C comes with many ready-made headers. A few you will use constantly:

HeaderGives you
<stdio.h>Input/output: printf, scanf, files
<stdlib.h>Memory, conversions: malloc, atoi
<string.h>String functions: strlen, strcpy
<math.h>Maths: sqrt, pow, sin

Creating your own header file

You can make your own header to share your functions. Put the prototypes in a .h file.

mymath.h
#ifndef MYMATH_H
#define MYMATH_H

int add(int a, int b);      // prototype only
int multiply(int a, int b);

#endif

Then write the actual code in a matching .c file, and use them from your main program.

Splitting a program across files

mymath.c
#include "mymath.h"
int add(int a, int b)      { return a + b; }
int multiply(int a, int b) { return a * b; }
main.c
#include <stdio.h>
#include "mymath.h"       // your own header, in quotes

int main() {
    printf("%d\n", add(3, 4));
    printf("%d\n", multiply(3, 4));
    return 0;
}
Output:
7
12

Compile both together, e.g. gcc main.c mymath.c -o app. The header lets main.c use functions whose code lives in another file.

Include guards

The #ifndef ... #define ... #endif lines you saw are an include guard. They stop a header from being pasted in twice, which would cause "redefinition" errors.

💡 Always add a guard

Every header you write should start with an include guard using a unique name (often the file name in capitals, like MYMATH_H).

Common mistakes

  • Putting full function code in the header instead of just prototypes.
  • Using < > for your own header instead of " ".
  • Forgetting the include guard, causing redefinition errors.
  • Forgetting to compile the matching .c file along with main.c.
🏋️ Practice

Create a utils.h with a prototype for int square(int), a utils.c with its code, and a main.c that uses it. Add an include guard and compile all together.

Summary

  • A header file (.h) holds declarations, not full code.
  • Use < > for standard headers and " " for your own.
  • Put prototypes in the .h, real code in the matching .c.
  • Always add an include guard to avoid double inclusion.
  • Compile all the .c files together to build the program.

Frequently Asked Questions

What is a header file in C?
A header file is a file ending in .h that contains declarations — function prototypes, macros, and type definitions — that you want to share across several source files. You bring its contents into a program with #include, so the compiler knows about those functions before they are used.
What is the difference between #include with angle brackets and quotes?
#include <file.h> tells the compiler to look in the standard system directories, used for library headers like stdio.h. #include "file.h" tells it to look in your own project folder first, used for headers you wrote yourself.
Why do we put declarations in a header and code in a .c file?
Separating them lets many source files share the same declarations without copying them. The .h file says "these functions exist and look like this," while the matching .c file holds the actual code. This keeps large programs organised and avoids duplication.
What is an include guard and why is it needed?
An include guard is the #ifndef / #define / #endif pattern at the top of a header. It stops the same header from being included more than once in a single compile, which would otherwise cause duplicate-definition errors.
Can I create my own header file in C?
Yes. You write your function prototypes and definitions in a .h file, add an include guard, and then use #include "yourfile.h" in any source file that needs them. This is exactly how large C projects share code between files.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.