tags:

views:

2902

answers:

6

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include statement.

  1. #include <filename>
  2. #include "filename"
+46  A: 

The #include directive causes a copy of a specified file to be included in the place of the directive. The two forms of the #include directive are:

  • #include <filename>
  • #include "filename"

The difference between these two is the location the preprocessor searches for the file to be included. If the file name is enclosed in quotes, the preprocessor searches in the same directory as the file being compiled for the file to be included. This method is normally used to include programmer defined headers. If the file name is enclosed in brackets - used for standard library headers - the search is performed in an implementation dependent manner, normally through predesignated directories.

quest49
The statement: "the preprocessor searches in the same directory..." may be true in practice but the standard states that the named source file is "searched for in an implementation-defined manner". See answer from piCookie.
Richard Corden
While your answer may appear to be "true", because this is how many implementations work by convention, you should take a close look at aib's and piCookie's answers. They both point out (backed by the wording of the C standard) that the real distinction is inclusion of a "header" versus inclusion of a "source file" (and no, this doesn't mean ".h" vs. ".c"). "Source file" in this context can be (and usually is, and almost always should be) a ".h" file. A header does not necessarily need to be a file (a compiler could e.g. include a header that is statically coded, not in a file).
Dan Moulding
A: 

I believe that headers included in double-quotes will be looked for the in same system paths as angle-bracketed includes if they are not found in the current directory.

mellis
A: 

The 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.

Arkadiy
+15  A: 

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. (Most, however, just treat it as a filename 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.

aib
+1, this is probably the most concise and correct answer here. According to the standard (which piCookie quotes from in his answer), the only *real* difference is "header" versus "source file". The search mechanism is implementation-defined either way. Using double quotes means that you intend to include a "source file", while angle brackets mean you intend to include a "header" which, as you say, may not be a file at all.
Dan Moulding
+39  A: 

The only way to know is to read your implementation's documentation.

In the C standard (find N1124.PDF), section 6.10.2 paragraphs 2 to 4 state:

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.

3) 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.

4) A preprocessing directive of the form

# include pp-tokens new-line

(that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms.145) The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.

h-char: any member of the source character set except the new-line character and >

q-char: any member of the source character set except the new-line character and "

piCookie
A: 

I have used both interchangeably and never noticed any difference (with Visual C++). My conclusion is that you can assume they are equal.