Header File Syntax: <>
vs ""
Why do some header files use angle brackets (<>
) while others use quotes (""
)?
The choice between angle brackets and quotes tells the compiler where to look for the header file. This is more than just a style choice - it affects how your program builds.
Angle Brackets (<>
)
When you use angle brackets, like #include <iostream>
, the compiler looks in the system include directories. These are folders where standard C++ headers and third-party libraries are installed. The exact locations depend on your compiler and operating system, but they're typically somewhere like:
/usr/include
on LinuxC:\Program Files\Microsoft Visual Studio\...\include
on Windows
Quotes (""
)
When you use quotes, like #include "Character.h"
, the compiler first looks in the same directory as your source file. If it can't find the header there, it then checks related directories in your project. Only if it still can't find the file will it look in the system directories.
Here's how you typically use each:
// System headers use angle brackets
#include <iostream>
#include <string>
#include <vector>
// Your own headers use quotes
#include "Character.h"
#include "Weapon.h"
The general rule is:
- Use angle brackets for headers that come with C++ or third-party libraries
- Use quotes for headers you've created as part of your project
Following this convention makes your code more maintainable because other developers can quickly understand where each header comes from. It also helps the compiler work more efficiently since it knows where to look first.
Header Files
Explore how header files and linkers streamline C++ programming, learning to organize and link our code effectively