The #include directive for local files
What you'll learn: How to use quotation marks to tell the preprocessor to pull in code from your own project files.
In previous lessons, you’ve likely used #include <stdio.h> to gain access to standard functions like printf. However, as your programs grow, you won't want to keep all your code in a single, massive file. You will want to organize your own custom functions and definitions into separate files. This is where the "local" version of the #include directive comes in.
The Power of Double Quotes
When you want to include a file that you created yourself—one that lives in the same folder as your source code—you don't use angle brackets (< >). Instead, you use double quotes (" ").
#include "my_settings.h"
#include <stdio.h>
int main() {
printf("The secret code is: %d", SECRET_NUMBER);
return 0;
}
Think of the preprocessor as a "Copy and Paste" robot. When it sees #include "my_settings.h", it pauses, opens your file named my_settings.h, copies everything inside it, and pastes it directly into your main file at that exact spot before the compiler even starts reading the code.
Brackets vs. Quotes: The Search Path
The difference between <file> and "file" tells the preprocessor where to look on your computer's "map":
- Angle Brackets
< >: These are for the "System Library." Use these for standard C tools. The preprocessor looks in specific pre-defined system folders where C was installed. - Double Quotes
" ": These are for "Local Files." The preprocessor starts looking in the current folder where your source code lives. If it doesn't find the file there, it will then look in the system folders as a backup.
The "Folder Relative" Analogy
Imagine you are looking for a book. Using angle brackets is like telling a librarian, "Go to the public library archives and find this reference manual." Using double quotes is like saying, "Check my backpack first; if it's not there, try the public library."
By using "my_file.h", you are telling C that this file is a custom part of your specific project, not a generic tool built into the language.
Key takeaway
Use #include "filename.h" with double quotes to include your own custom files located within your project folder.