3

I am learning C and I am unsure where to include files. Basically I can do this in .c or in .h files:

Option 1

test.h

int my_func(char **var);

test.c

#include <stdio.h>
#include "test.h"
int my_func(char **var) {printf("%s\n", "foo");}
int main() {...}

Option 2

test.h

#include <stdio.h>
int my_func(char **var);

test.c

#include "test.h"
int my_func(char **var) {printf("%s\n", "foo");}
int main() {...}

With option 2 I would only need to include test.h in whatever .c file I need the library. Most of the examples I see use option 1.

Are there some general rules when to do what or is this a question of personal preferences?

Spliffster
  • 6,959
  • 2
  • 24
  • 19
  • Note that this often depend on whether your `test.h` file actually declares something that depends on `stdio.h`, which in your case it doesn't. But if the .h file contained references, e.g. `EOF` or a `FILE*` from stdio.h , many would have a different preference on whether to use Option 1 or 2. – nos Mar 25 '13 at 13:06
  • I want to use stdio functionality in my functions. I have changed the example by adding printf in the function. Thank you for pointing this out. – Spliffster Mar 25 '13 at 19:02
  • 1
    That's not what I meant. Your .h file still doesn't depend on anything in stdio.h. Your .c file might, but it's the .h file that would matter. Whoever includes your test.h file (except test.c), doesn't need to know that those functions use stdio.h in your case. If test.h had a function `int my_func(FILE *output)` , it's a different matter – nos Mar 25 '13 at 19:37

7 Answers7

5

Don't use includes, you don't need.

I'd choose something like "Option 1". Why "something like" ? Because I'd create a separate file for the main and I'd keep all declaraions inside the .h and all definitions inside the corresponding .c.

Of course, both options are valid.

If you want to include only one header in your main, you can just create a header file, containing only includes - that's a common practice. This way, you can include only one header, instead of several.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
3

I tend to prefer Option 1, as cyclic dependencies will come and bite you very quickly in option 2, and reducing the input size is the best way to guarantee faster compile times. Option 2 tends towards including everything everywhere, whether you really need it or not.

That said, it might be best to experiment a little with what works for structuring your projects. Hard and fast rules tend to not apply universally to these kinds of questions.

Gian
  • 13,735
  • 44
  • 51
2

both options are correct. the C standard allows both solutions

All C standard headers must be made such that they can be included several times and in any order:

Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once

(From Preprocessor #ifndef)

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

I don't think that there is a universal rule (from a "grammatical" point of view, both your options are correct and will work). What is often done is to include in the .h file the headers you need for the library (as in your option 1), either because you'll need them when working with the library (thus avoiding always including the same set of header files in your .c files, which is more error prone), or because they are mentioned in the .h file itself (e.g., if you use a int32_t as type in the function prototypes of the .h files, you will of course need to include <stdint.h> in the .h file).

Ale
  • 1,727
  • 14
  • 26
0

I prefer to use includes in c file. If your program is getting bigger you might forgot to include something in one header file, but it is included in one other you use. By including them in c-file you won't lose includes, while editing other files.

Justus Schmidt
  • 130
  • 1
  • 8
0

I prefer Option 1. I want to know what I used in my project, and in much time, Option 1 works more effective than Option 2 in time and efficiency.

hzp
  • 11
0

There is no rule specifying you have following a particular fashion. Even if you include / not include in test.c file, it is not going to bother much, provided you include it in test.h file and include that test.h file in test.c. Hope you are clear with that.

This is because, you have preprocessor directives like #ifndef, #define, #endif. They are called Include guards. These are used in the inbuild header files. However, when you include a file written by you, either go with your option 2, or use include guards to be safe.

The include guards work as follows. #ifndef ANYTHING #define ANYTHING .. .. .. #endif

So when you include for the first time, ANYTHING is not yet defined. So ifndef returns true, then ANYTHING gets defined, and so...on.. But the next time if you include the same file ( by mistake) ifndef would return a false since ANYTHING is now defined and so the file would not be included at all. This protection is necessary to avoid duplicate declarations of variable present in the header file. That would give you a compilation error.

Hope that helps Cheers

shar
  • 1,988
  • 2
  • 18
  • 25