The difference between #include <iostream>
and #include "file.h"
lies in how the preprocessor searches for the files being included in a C++ program.
Standard Library Header (#include <iostream>
)
When you use angle brackets (<>
), the preprocessor searches for the file in the standard include directories. These are predefined locations where the compiler expects to find the system and standard library headers. For example, #include <iostream>
is a header file in the C++ Standard Library that allows you to use input and output functionality like std::cin
, std::cout
, and std::cerr
. Since it is a system-provided file, the preprocessor doesn’t look in the current directory or user-defined paths for it.
User-Defined Header (#include "file.h"
)
When you use double quotes (""
), the preprocessor first searches for the file in the current directory (where your source code is located). If the file is not found there, it then checks the standard include directories. This method is typically used for user-defined or project-specific header files. For example, if you have a custom file named file.h
in the same directory as your program, #include "file.h"
will include it.
Key Difference
#include <...>
: Searches only system or standard paths.#include "..."
: Searches the current directory first, then falls back to standard paths.
In summary, #include <...>
is for system libraries, while #include "..."
is primarily for user-defined or local files.