9

Possible Duplicate:
What is the difference between #include <filename> and #include “filename”?

Why doesn't the compiler complain when I write the following:

#include "stdio.h" 

Shouldn't it be

#include <stdio.h> 

instead, because stdio.h is actually stored in a library folder and not in the folder of the translation unit? Why does it work anyway?

7
  • 1
    "..." looks locally first, and then elsewhere. Commented Dec 3, 2012 at 13:40
  • 1
    no, it should really be #inlcude <cstdio> Commented Dec 3, 2012 at 13:40
  • 1
    stackoverflow.com/questions/21593/… Commented Dec 3, 2012 at 13:41
  • @111111, it's not the point of the question, is it? Commented Dec 3, 2012 at 13:46
  • @aleguna which is why it is a comment Commented Dec 3, 2012 at 13:47

3 Answers 3

10

The difference between "" and <> isn't much. Both search for the header in implementation-defined places1, 2. The difference is that if that search fails for "", the search happens as if it was using <>. (§16.2)

Basically, this means that if <> finds a header with a certain name, "" does not fail to find a header with the same name3.


1 These implementation-defined places do not have to be the same for both forms.

2 There is no requirement that one of these search library folders and the other search the folder of the TU. The compiler is allowed to search the whole filesystem and even google for it if it wants.

3 This does not mean that they always find the same header, though.

Sign up to request clarification or add additional context in comments.

9 Comments

There is a requirement, though, that if #include "xxx" fails, the compiler reprocess it as if it had been #include <xxx>.
@James That's what I intended the third sentence to mean. Do you think I should rephrase it, or did you miss that?
+1 for googling source files :-)
I think the significant reason for the "places" being so loosely specified is that header "files" don't have to be actual files, let alone in an actual filesystem. You could write a conforming C++ implementation over some kind of version control or whatnot, just as long as it can figure out what bytes the nominal "file" contains. And of course in practice both locations depend on command-line options.
@SteveJessop, yeah wouldn't it be nice to be able to include different revisions of the same file from mercurial, or whatever vcs.. %) #include "MyClass.h.99fdadcf54"
|
1

This is because of how the include syntax is defined.

#include <cstdio> means that the compiler should include the standard library cstdio

#include "cstdio" means the compiler should try to find the file "cstdio", looking primarily in the current directory and using the location of the standard libraries as a fallback.

1 Comment

Whether or not either #include <> or #include "" look in the current directory first or at all is entirely implementation defined.
1

"" versus <> only changes the order of lookup.

so with

#include "stdio.h" 

precompiler will start lookup from the directory of translation unit, and then move to predefined "include" directories

Whereas

#include <stdio.h> 

Is other way around

8 Comments

@CharlesBailey But in practice, it is generally accepted that the "..." include should first look in the directory which contains the file with the include. (On the other hand, I don't think any compiler will look in the current directory for an include, unless you pass it -I. or the file it is reading is in the current directory.)
@CharlesBailey, where did I say anything about current directory?
@aleguna: Sorry, for "current directory" read "directory containing the initial source file for the translation unit being compiled". I was being lazy.
Whether or not either #include <> or #include "" look in the directory containing the initial source file for the translation unit being compiled or the directory containing the source file containing the include directive being interpreted, either first or at all is entirely implementation defined.
@CharlesBailey, theory and references to standard are all good and well. But it practice all mainstream [pre]compilers work as I described. And I'm sure the question was about real world behaviour, not about what's in the standard
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.