C Header Files

What is a Header File?

A header file is a file with a .h extension that contains declarations of functions, variables, and macros.

It allows multiple source files to share common code.

#include Directive

The #include directive is used to include header files in your program.

Loading...
Output:

Standard Header Files

C provides many built-in header files.

  • stdio.h – input/output
  • stdlib.h – memory management
  • string.h – string functions

Custom Header Files

You can create your own header files to organize code.

Loading...
Output:

Including Custom Headers

Use double quotes to include your own header files.

Loading...
Output:

Function Declarations

Header files usually contain function declarations (prototypes).

The actual implementation is written in a .c file.

Loading...
Output:

Using the Header in main

Include the header file in your main program to use its functions.

Loading...
Output:

Header Guards

Header guards prevent multiple inclusion of the same file.

Loading...
Output:

Why Use Header Files?

Header files are essential for organizing large programs.

  • Separate code into multiple files
  • Improve readability
  • Enable code reuse

Common Mistakes

Be careful with these:

  • Forgetting header guards
  • Missing function definitions
  • Incorrect include paths

Real-World Structure

A typical C project uses multiple files.

Loading...
Output:

Practice

Create a header file with a function and use it in main.

Loading...
Output:

Need Help?