Course contentsShow
← C
Lesson 19 of 1,2191. FoundationsFree lesson

What is a Header File?

What is a Header File?

What you'll learn: You will understand how header files act as a "menu" that tells your program which pre-made tools are available to use.

The Restaurant Analogy

Imagine walking into a restaurant. Before you can eat, the server hands you a menu. The menu doesn’t contain the actual food or the chef’s secret recipes; it simply lists the names of the dishes and a brief description of what they are.

In C, a header file is that menu. It is a file (ending in .h) that contains a list of functions and tools that have already been written by other programmers. By "reading" the header file, your program learns the names of these tools and how to use them correctly, even though the complex code behind them is hidden away in a different kitchen.

Telling the Compiler Where to Look

When you start a C program, you almost always see this line at the very top:

#include <stdio.h>

int main() {
    printf("Hello, world!");
    return 0;
}

In this example, stdio.h is a header file. The name stands for Standard Input/Output.

Without that top line, the C compiler would look at printf and get confused. It would be like a customer ordering a "Filet Mignon" at a restaurant that doesn't have a menu—the waiter wouldn't know what they are talking about. By including the header, you are telling the computer: "Look inside this specific list to find the definition for the tools I'm about to use."

Why do we use them?

We use header files to keep our code clean and organized. Instead of writing thousands of lines of code to handle complex tasks like printing text to a screen or calculating square roots every single time we start a project, we simply "include" the header file that already knows how to do it. It allows us to stand on the shoulders of the programmers who came before us, using their work as building blocks for our own creations.

Key takeaway

A header file is a reference list that introduces your program to external tools and functions.