Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between angle bracket < > and double quotes " " while including header files in C++? [duplicate]

Tags:

c++

c

c++11

People also ask

What is the difference between #include and include <>?

The difference between the two types is in the location where the preprocessor searches for the file to be included in the code. #include<> is for pre-defined header files. If the header file is predefined then simply write the header file name in angular brackets.

What type of header is specified when using angle brackets with include?

Angular brackets are used in the header file section for system headers. During compilation all the header files are added to the . c/.

What is difference between <> and in C?

The difference is that when it's enclosed in quotes, the compiler will look in the local directory, but with <> , it won't. If you want to get technical, the C standard doesn't guarantee that, but it's how essentially all compilers work.

What are the header files in C?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.


It's compiler dependent. That said, in general using " prioritizes headers in the current working directory over system headers. <> usually is used for system headers. From to the specification (Section 6.10.2):

A preprocessing directive of the form

  # include <h-char-sequence> new-line

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

A preprocessing directive of the form

  # include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

  # include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original directive.

So on most compilers, using the "" first checks your local directory, and if it doesn't find a match then moves on to check the system paths. Using <> starts the search with system headers.


When you use angle brackets, the compiler searches for the file in the include path list. When you use double quotes, it first searches the current directory (i.e. the directory where the module being compiled is) and only then it'll search the include path list.

So, by convention, you use the angle brackets for standard includes and the double quotes for everything else. This ensures that in the (not recommended) case in which you have a local header with the same name as a standard header, the right one will be chosen in each case.