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.
<>
)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""
)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:
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.
Answers to questions are automatically generated and may not have been reviewed.
Explore how header files and linkers streamline C++ programming, learning to organize and link our code effectively