What is the difference between using angle brackets and quotes in an include
directive?
#include
#include "filename"
What differs is the location in which the preprocessor searches for the included file.
#include
#include "filename" The preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include
A more complete description is available in the GCC documentation on search paths.
The only way to know is to read your implementation's documentation.
In the C standard, section 6.10.2, paragraphs 2 to 4 state:
A preprocessing directive of the form #include
The sequence of characters between < and > uniquely refer to a header, which isn't necessarily a file. Implementations are pretty much free to use the character sequence as they wish. (Mostly, however, just treat it as a file name and do a search in the include path, as the other posts state.)
If the #include "file"
form is used, the implementation first looks for a file of the given name, if supported. If not (supported), or if the search fails, the implementation behaves as though the other (#include <file>
) form was used.
Also, a third form exists and is used when the #include
directive doesn't match either of the forms above. In this form, some basic preprocessing (such as macro expansion) is done on the "operands" of the #include
directive, and the result is expected to match one of the two other forms.
<
and >
as the key to index into the library.
Some good answers here make references to the C standard but forgot the POSIX standard, especially the specific behavior of the c99 (e.g. C compiler) command.
According to The Open Group Base Specifications Issue 7,
-I directory Change the algorithm for searching for headers whose names are not absolute pathnames to look in the directory named by the directory pathname before looking in the usual places. Thus, headers whose names are enclosed in double-quotes ( "" ) shall be searched for first in the directory of the file with the #include line, then in directories named in -I options, and last in the usual places. For headers whose names are enclosed in angle brackets ( "<>" ), the header shall be searched for only in directories named in -I options and then in the usual places. Directories named in -I options shall be searched in the order specified. Implementations shall support at least ten instances of this option in a single c99 command invocation.
So, in a POSIX compliant environment, with a POSIX compliant C compiler, #include "file.h"
is likely going to search for ./file.h
first, where .
is the directory where is the file with the #include
statement, while #include <file.h>
, is likely going to search for /usr/include/file.h
first, where /usr/include
is your system defined usual places for headers (it's seems not defined by POSIX).
c99
— which is the POSIX name for the C compiler. (The POSIX 2008 standard could hardly refer to C11; the 2013 update to POSIX 2008 did not change the C standard that it referred to.)
-L
.
The exact behavior of the preprocessor varies between compilers. The following answer applies for GCC and several other compilers.
#include <file.h>
tells the compiler to search for the header in its "includes" directory, e.g. for MinGW the compiler would search for file.h
in C:\MinGW\include\ or wherever your compiler is installed.
#include "file"
tells the compiler to search the current directory (i.e. the directory in which the source file resides) for file
.
You can use the -I
flag for GCC to tell it that, when it encounters an include with angled brackets, it should also search for headers in the directory after -I
. GCC will treat the directory after the flag as if it were the includes
directory.
For instance, if you have a file called myheader.h
in your own directory, you could say #include <myheader.h>
if you called GCC with the flag -I .
(indicating that it should search for includes in the current directory.)
Without the -I
flag, you will have to use #include "myheader.h"
to include the file, or move myheader.h
to the include
directory of your compiler.
GCC documentation says the following about the difference between the two:
Both user and system header files are included using the preprocessing directive ‘#include’. It has two variants: #include
#include
-ing file's current directory, the quote directories (the paths to check for the #include "foo.h"
style includes), and the system directories (the paths to check for the #include <bar.h>
style includes).
It does:
"mypath/myfile" is short for ./mypath/myfile
with .
being either the directory of the file where the #include
is contained in, and/or the current working directory of the compiler, and/or the default_include_paths
and
<mypath/myfile> is short for <defaultincludepaths>/mypath/myfile
If ./
is in <default_include_paths>
, then it doesn't make a difference.
If mypath/myfile
is in another include directory, the behavior is undefined.
#include "mypath/myfile"
is not equivalent to #include "./mypath/myfile"
. As piCookie's answer says, double quotes tell the compiler to search in an implementation-defined manner -- which includes searching in the places specified for #include <...>
. (Actually, it probably is equivalent, but only because, for example, /usr/include/mypath/myfile
can be referred to as /usr/include/./mypath/myfile
-- at least on Unix-like systems.)
defaultincludepaths
, as opposed to giving another meaning to the .
(as referred to above). This has the expected consequence that both #include "..."
and #include <...>
search in dirpath
The <file>
include tells the preprocessor to search in -I
directories and in predefined directories first, then in the .c file's directory. The "file"
include tells the preprocessor to search the source file's directory first, and then revert to -I
and predefined. All destinations are searched anyway, only the order of search is different.
The 2011 standard mostly discusses the include files in "16.2 Source file inclusion".
2 A preprocessing directive of the form # include
Note that "xxx"
form degrades to <xxx>
form if the file is not found. The rest is implementation-defined.
-I
business is specified?
-I
.
By the standard - yes, they are different:
A preprocessing directive of the form #include
Note that the standard does not tell any relation between the implementation-defined manners. The first form searches in one implementation-defined way, and the other in a (possibly other) implementation-defined way. The standard also specifies that certain include files shall be present (for example, <stdio.h>
).
Formally you'd have to read the manual for your compiler, however normally (by tradition) the #include "..."
form searches the directory of the file in which the #include
was found first, and then the directories that the #include <...>
form searches (the include path, eg system headers).
At least for GCC version <= 3.0, the angle-bracket form does not generate a dependency between the included file and the including one.
So if you want to generate dependency rules (using the GCC -M option for exemple), you must use the quoted form for the files that should be included in the dependency tree.
(See http://gcc.gnu.org/onlinedocs/cpp/Invocation.html )
Thanks for the great answers, esp. Adam Stelmaszczyk and piCookie, and aib.
Like many programmers, I have used the informal convention of using the "myApp.hpp"
form for application specific files, and the <libHeader.hpp>
form for library and compiler system files, i.e. files specified in /I
and the INCLUDE
environment variable, for years thinking that was the standard.
However, the C standard states that the search order is implementation specific, which can make portability complicated. To make matters worse, we use jam, which automagically figures out where the include files are. You can use relative or absolute paths for your include files. i.e.
#include "../../MyProgDir/SourceDir1/someFile.hpp"
Older versions of MSVS required double backslashes (\\), but now that's not required. I don't know when it changed. Just use forward slashes for compatibility with 'nix (Windows will accept that).
If you are really worried about it, use "./myHeader.h"
for an include file in the same directory as the source code (my current, very large project has some duplicate include file names scattered about--really a configuration management problem).
Here's the MSDN explanation copied here for your convenience).
Quoted form The preprocessor searches for include files in this order: In the same directory as the file that contains the #include statement. In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files. Along the path that's specified by each /I compiler option. Along the paths that are specified by the INCLUDE environment variable. Angle-bracket form The preprocessor searches for include files in this order: Along the path that's specified by each /I compiler option. When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.
For #include ""
a compiler normally searches the folder of the file which contains that include and then the other folders. For #include <>
the compiler does not search the current file's folder.
<filename>
and "filename"
search for implementation-defined places.
When you use #include <filename>
, the pre-processor looking for the file in directory of C\C++ header files (stdio.h\cstdio, string, vector, etc.). But, when you use #include "filename"
first, the pre-processor is looking for the file in the current directory, and if it doesn't here - the pre-processor will look for it in the directory of C\C++ header files.
#include
directive isn't strictly related to files at all.
An #include with angle brackets will search an "implementation-dependent list of places" (which is a very complicated way of saying "system headers") for the file to be included.
An #include with quotes will just search for a file (and, "in an implementation-dependent manner", bleh). Which means, in normal English, it will try to apply the path/filename that you toss at it and will not prepend a system path or tamper with it otherwise.
Also, if #include "" fails, it is re-read as #include <> by the standard.
The gcc documentation has a (compiler specific) description which although being specific to gcc and not the standard, is a lot easier to understand than the attorney-style talk of the ISO standards.
zlib.h
in my 'user' search paths, and a different version exists in the system search path, then does #include <zlib.h>
include the system version and #include "zlib.h"
include my own?
Many of the answers here focus on the paths the compiler will search in order to find the file. While this is what most compilers do, a conforming compiler is allowed to be preprogrammed with the effects of the standard headers, and to treat, say, #include <list>
as a switch, and it need not exist as a file at all.
This is not purely hypothetical. There is at least one compiler that work that way. Using #include <xxx>
only with standard headers is recommended.
#include "fname"
where the file name contained periods, but a conforming implementation must accept e.g. ` #include <float.h>` without regard for whether float.h
would be a valid file name.
#include <> is for predefined header files
If the header file is predefined then you would simply write the header file name in angular brackets, and it would look like this (assuming we have a predefined header file name iostream):
#include <iostream>
#include " " is for header files the programmer defines
If you (the programmer) wrote your own header file then you would write the header file name in quotes. So, suppose you wrote a header file called myfile.h
, then this is an example of how you would use the include directive to include that file:
#include "myfile.h"
#include "filename" // User defined header
#include <filename> // Standard library header.
Example:
The filename here is Seller.h
:
#ifndef SELLER_H // Header guard
#define SELLER_H // Header guard
#include <string>
#include <iostream>
#include <iomanip>
class Seller
{
private:
char name[31];
double sales_total;
public:
Seller();
Seller(char[], double);
char*getName();
#endif
In the class implementation (for example, Seller.cpp
, and in other files that will use the file Seller.h
), the header defined by the user should now be included, as follows:
#include "Seller.h"
#include <abc.h>
is used to include standard library files. So the compiler will check in the locations where standard library headers are residing.
#include "xyz.h"
will tell the compiler to include user-defined header files. So the compiler will check for these header files in the current folder or -I
defined folders.
In C++, include a file in two ways:
The first one is #include which tells the preprocessor to look for the file in the predefined default location. This location is often an INCLUDE environment variable that denotes the path to include files.
And the second type is #include "filename" which tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations user have set up.
The #include <filename>
is used when a system file is being referred to. That is a header file that can be found at system default locations like /usr/include
or /usr/local/include
. For your own files that needs to be included in another program you have to use the #include "filename"
syntax.
Form 1 - #include < xxx >
First, looks for the presence of header file in the current directory from where directive is invoked. If not found, then it searches in the preconfigured list of standard system directories.
Form 2 - #include "xxx"
This looks for the presence of header file in the current directory from where directive is invoked.
The exact search directory list depends on the target system, how GCC is configured, and where it is installed. You can find the search directory list of your GCC compiler by running it with -v option.
You can add additional directories to the search path by using - Idir, which causes dir to be searched after the current directory (for the quote form of the directive) and ahead of the standard system directories.
Basically, the form "xxx" is nothing but search in current directory; if not found falling back the form
#include "header.h"
form is not accurate, @personal_cloud. I consider the answer by piCookie and Yann Droneaud to be most relevant as they identify where their information comes from. I don't find the top-voted answer to be entirely satisfactory, either.
The simple general rule is to use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files. Most compilers do it this way.
1.9 — Header files explains in more detail about pre-processor directives. If you are a novice programmer, that page should help you understand all that. I learned it from here, and I have been following it at work.
#include
is used when you want to use the header file of the C/C++ system or compiler libraries. These libraries can be stdio.h, string.h, math.h, etc.
#include "path-to-file/filename"
is used when you want to use your own custom header file which is in your project folder or somewhere else.
For more information about preprocessors and header. Read C - Preprocessors.
the " < filename > " searches in standard C library locations whereas "filename" searches in the current directory as well.
Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.
#include <filename>
The preprocessor searches in an implementation-dependent manner. It tells the compiler to search directory where system header files are held.
This method usually use to find standard header files.
#include "filename"
This tell compiler to search header files where program is running. If it was failed it behave like #include
This method usually used for identify user defined header files(header files which are created by user). There for don't use this if you want to call standard library because it takes more compiling time than #include
In general the difference is where the preprocessor searches for the header file:
#include is a preprocessor directive to include header file. Both #include are used to add or include header file in the program, but first is to include system header files and later one for user defined header files.
#include
Check the gcc docs gcc include files
#include <file>
Includes a file where the default include directory is.
#include "file"
Includes a file in the current directory in which it was compiled. Double quotes can specify a full file path to a different location as well.
To see the search order on your system using gcc, based on current configuration , you can execute the following command. You can find more detail on this command here
cpp -v /dev/null -o /dev/null
Apple LLVM version 10.0.0 (clang-1000.10.44.2) Target: x86_64-apple-darwin18.0.0 Thread model: posix InstalledDir: Library/Developer/CommandLineTools/usr/bin "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 409.12 -v -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/10.0.0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -I/usr/local/include -fdebug-compilation-dir /Users/hogstrom -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fencode-extended-block-signature -fobjc-runtime=macosx-10.14.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -traditional-cpp -o - -x c /dev/null clang -cc1 version 10.0.0 (clang-1000.10.44.2) default target x86_64-apple-darwin18.0.0 ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks" #include "..." search starts here: #include <...> search starts here: /usr/local/include /Library/Developer/CommandLineTools/usr/lib/clang/10.0.0/include /Library/Developer/CommandLineTools/usr/include /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory) End of search list.
""
will search ./
first. Then search the default include path. You can use command like this to print the default include path:
gcc -v -o a a.c
Here are some examples to make thing more clear: the code a.c works
// a.c
#include "stdio.h"
int main() {
int a = 3;
printf("a = %d\n", a);
return 0;
}
the code of b.c works too
// b.c
#include <stdio.h>
int main() {
int a = 3;
printf("a = %d\n", a);
return 0;
}
but when I create a new file named stdio.h
in current directory
// stdio.h
inline int foo()
{
return 10;
}
a.c
will generate compile error, but b.c
still works
and "", <> can be used together with the same file name. since the search path priority is different. so d.c
also works
// d.c
#include <stdio.h>
#include "stdio.h"
int main()
{
int a = 0;
a = foo();
printf("a=%d\n", a);
return 0;
}
The implementation-defined warnings generated by the compiler can (and will) treat system libraries differently than program libraries.
So
#include <myFilename>
-- which in effect declares that myFilename is in the system library location -- may well (and probably will) hide dead code and unused variable warnings etc, that would show up when you use:
#include "myFilename"
#include
, but by the headers being in specific system directories. See gcc and Microsoft docs on the subject.
#include
does not trigger the system header behavior, as your answer claims. The only exception is with VS with the /external:anglebrackets
flag, which is non-default.
Success story sharing
<>
for headers included with the implementation,""
for everything else. But it's made clear that this is a guideline only, the search paths for both cases are implementation defined other than the fact""
will fall back to<>
if it can't be found.