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

The `#include` directive

The #include directive

What you'll learn: You will understand how to borrow pre-written tools from the C library to give your programs basic powers like printing text.

The library analogy

Imagine you are writing a manual on how to build a bookshelf. Instead of spending fifty pages explaining how to manufacture a screwdriver from raw steel, you simply add a note at the top that says: "Refer to the Tool Kit Catalog for instructions on using a screwdriver."

In C, the #include directive does exactly that. It tells the computer, "Before you start reading my code, go grab this specific 'catalog' of tools so I can use them." Without this directive, your program is a blank slate with no way to talk to the screen, the keyboard, or the hard drive.

Bringing in the tools

The most common line you will see at the very top of a C file is #include <stdio.h>.

Let’s break that down:

  • The # symbol: This is a signal to the "preprocessor." It tells C that this line needs to be handled before the actual program is built.
  • include: This is the command to literally copy and paste the contents of another file into yours.
  • <stdio.h>: This stands for Standard Input/Output. It is a file that contains the blueprints for basic tasks, like displaying text (Output) or reading a button press (Input).

What it looks like in code

When you include a header file (files ending in .h), you gain access to functions you didn't have to write yourself.

// We "include" the Standard I/O library to use the printf tool
#include <stdio.h>

int main() {
    // printf is a tool defined inside stdio.h
    printf("Hello, world!");
    return 0;
}

If you deleted that first line, the computer would look at printf and say, "I have no idea what this is." By including the file, you are giving your program a vocabulary.

Why do we do this?

C is designed to be lean and fast. If every C program automatically included every tool ever invented, the files would be massive and slow. Instead, C starts with nothing. You only #include the specific toolboxes you need for the job at hand, keeping your program lightweight and efficient.

Key takeaway

The #include directive imports external files containing pre-written code, allowing you to use standard tools like printf without writing them from scratch.