Course contentsShow
← C
Lesson 765 of 1,21911. The Preprocessor and BuildFree lesson

The `#include` directive for standard headers

The #include directive for standard headers

What you'll learn: How to use the #include directive to bring pre-written functionality from the C Standard Library into your own programs.

The Copy-Paste Assistant

Imagine you are writing a cookbook. Instead of writing out the universal instructions for "how to boil water" in every single recipe, you simply stick a sticky note that says, "Insert the standard boiling instructions here."

In C, the #include directive works exactly like that sticky note. It is an instruction for the preprocessor—a program that processes your code before it is actually compiled. When the preprocessor sees #include, it literally copies the entire content of a specific file and pastes it into your code at that exact spot.

Angle Brackets and Standard Headers

When we want to use tools that are built into the C language itself (like the ability to print text to the screen), we use Standard Headers. To tell the preprocessor to look in the system's official folders for these files, we wrap the filename in angle brackets: < >.

The most common header you will see is stdio.h (Standard Input/Output). It contains the "blueprints" for functions like printf. Without including this header, the compiler wouldn't understand what printf means.

// The preprocessor replaces this line with the contents of stdio.h
#include <stdio.h>

int main() {
    // Because we included stdio.h, we can now use printf
    printf("Hello, C world!");
    return 0;
}

Why do we do this?

You might wonder why C doesn't just include everything automatically. C is designed to be lean and fast. By requiring you to manually #include only what you need, the compiler avoids processing thousands of lines of unnecessary code. If you only need math functions, you include <math.h>. If you don't need them, your program stays small and efficient.

Remember: The #include line does not end with a semicolon. This is because it is a preprocessor directive, not a standard C statement. It is a special command that happens before the "real" compiling begins.

Key takeaway

The #include <header.h> directive tells the preprocessor to copy-paste standard library definitions into your file so you can use built-in functions.