Gam
Gam

Reputation: 694

Should I use #include <file.h> or "file.h"?

There are two ways to include a file in C :

#include <headerpath/header.h>

or

#include "headerpath/header.h"

The first one will look for the file by using a directory known by the compiler, so we can include standard files without knowing where they are. The second way will look for the file by using only the path between quotes. (if the search fails, the compiler tries the first way instead).

We have the possibility to add one or more directories into the directories's list that the compiler know (first way). For example with gcc we have the -I option.

So at the end, these two following codes are equivalent (path_to_header is a directory) :

1)

#include "path_to_header/header.h"

int     main(void)
{
    return (0);
} // Compiling with : gcc main.c

2)

#include <header.h>

int     main(void)
{
    return (0);
} // Compiling with : gcc main.c -I path_to_header

So my questions are : With my own header files for example, should I use the 1) or the 2) ? Why ? Maybe it's just a personal choice ? Are there different situations to know ?

Thank's for reading :)

Edit : I'm not looking for the difference between the two ways (I think I understood them as I explained, thanks to this post), I wanted to know if there are some special situations to know about, maybe for group work or using different compilers for the same program ... Maybe I do not know how to formulate my thoughts (or it's a silly question without real answer), I have to try to know :).

Upvotes: 4

Views: 7106

Answers (4)

hterrolle
hterrolle

Reputation: 27

In my case, building with android.mw you can use <> in all the cases.

But if you are using CMakeLists every include under the JNI lib must use "". If not, you get "file not found".

Upvotes: 0

bbaassssiiee
bbaassssiiee

Reputation: 6782

For headers of the standard libraries (which probably are precompiled) use:

#include <stdio.h>

For headers of your project use:

#include "project/header.h"

Use the option -I on the command line for additional libraries.

Upvotes: 6

Max
Max

Reputation: 22325

According to the C standard the only standard difference between them is that #include <...> includes a header while #include "..." includes a source file (and falls back to the <...> behavior if no source file is found). All other differences are implementation-defined.

The distinction is important because, for example, a standard header like stdlib.h might not actually be a file, and is instead injected by the compiler at compile time.

For your own code, since you won't have such header magic, and should know exactly which source files you want included from your own work and which you want the compiler to handle (system libraries and such) you should only use <...> for includes that are not part of your project's file structure.

Upvotes: 3

Gabriel Magri
Gabriel Magri

Reputation: 182

If your own header files are in a defined path, like the same folder with your files that use your headers you must use this way "header.h". You must use < header.h > when the header is a system header that is not with your sources where you are including it.

Upvotes: 1

Related Questions