Reputation: 7209
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?
Upvotes: 3
Views: 1721
Reputation: 2078
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
Upvotes: 0
Reputation: 11
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.
Upvotes: 0
Reputation: 140
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.
Upvotes: 0
Reputation: 1819
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).
Upvotes: 0
Reputation: 38163
Don't use include
s, 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 include
s - that's a common practice. This way, you can include only one header, instead of several.
Upvotes: 5
Reputation: 13945
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.
Upvotes: 3
Reputation: 43518
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)
Upvotes: 2