4

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

Gam
  • 684
  • 1
  • 8
  • 17
  • 1
    use <> when including a system header and "" when including a header which path is relative to the path of the current source file. – hetepeperfan Sep 18 '17 at 15:06
  • 1
    Possible duplicate of [What is the difference between #include and #include "filename"?](https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) – ad absurdum Sep 18 '17 at 15:11
  • `#include "path_to_header/header.h"` defeats "so we can include standard files without knowing where they are." . Sometimes a bit of mystery is good. Use #3 `#include "header.h"`. – chux - Reinstate Monica Sep 18 '17 at 15:17

3 Answers3

6

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.

bbaassssiiee
  • 6,013
  • 2
  • 42
  • 55
2

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.

Max
  • 21,123
  • 5
  • 49
  • 71
1

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.