What’s the difference between #include <filename> and #include "filename"?

Asked

Viewed 1,239 times

9

Why do we use

#include <filename>

and

#include "filename"

When to use each?

1 answer

10


#include <filename>

It is used for standard language libraries. Generally the compiler already knows where the headers of what is part of the language are. Obviously this can be configured. But how it will be done is implementation problem.

Of course nothing prevents putting other things together, but it is not recommended.

So

#include <filename.h>

you should probably access /usr/include/file.h or C:\Program Files\Microsoft SDKs\Windows\v10.0\Include

In C++ you usually do not need to use the .h, at least for language-specific headers and not those that are "imported" from C, although it has names without the .h to access C headers (ex.: cstdio in place of stdio.h).

Note then that it is not necessary to have an existing file name, the name written needs to define what the header is, how it will find what should be included depends on the compiler, and it is common to have these names that do a mapping to other files (e.g..: cstring -> string.h).

#include "filename"

It is used for third-party libraries and their own code. In general it looks in the folder of the one being compiled. There are directives in the compiler to add other locations. The exact implementation is also dependent on the compiler.

If it is not possible to do this search, it decays to the previous form. If you do not find something there will be an error.

So

#include "file.h"

must access ./file.h.

In C++ some people prefer to use .hpp to make it clear that it is compatible with C++ and not C.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.