Reputation: 5730
I really want to know right way of using header file.
Belows are two ways of using header file which i think either of them is good way.
notice : Destroy.c also use stdio.h, stdlib.h
1.
2.
Please advice me. Thanks
Upvotes: 5
Views: 8598
Reputation: 51
Simply, you should include header files only whenever you need to call a function/macro that your c file is using its prototype/definition. So in your case, if you're using any of the stdio/stdlib/struct libraries predefined functions -like printf- in the Destroy() function then make sure you include them, and if you're not, then you should not include them, although you're compiler might not even warn you about it.
In addition, if this is the only function you're calling in the main() function, then you should not include those header files again in the main.c file.
Hope this helps :)
Upvotes: 0
Reputation: 70893
Do not establish unnecessary dependencies!
There is no need to include any system headers into destroy.h
. If needed by code in destroy.c
include them there.
Include system/library headers 1st. There are very rare conditions to not stick to this rule.
Update on why inlcude system header 1st:
The system headers declare the "frame-work" the program wants to use. So the program should "know" about this "frame-work" before declaring its own stuff, as it might rely on what the system provides.
The systen mostly never relies on what the program provides.
Same for libraries, from the program's perspective, those are just additions to the system.
Upvotes: 4
Reputation: 108641
It is a good practice to mention #include
lines in each module in a way that helps document that module. That way the next person to work on your code can look at it and see, for example, "aha, this module uses stdlib." It's a way of making your dependencies visible, both to compiler and to programmer.
So, if you use the capabilities of stdlib
in your Destroy.c
module mention it in an #include
line. If you don't, leave it out.
If your main.c
program uses the capabilities in your Destroy.c
module, include Destroy.h
in your main.c
.
The same is true (in my opinion) when you decide whether to nest the #include
of a system header file inside one of your header files. If the stuff in your header file itself makes use of the stuff in the system header file, include it.
This header-file pattern:
#ifndef MODULE_H
#define MODULE_H
/* lots of stuff */
#endif
has the purpose of making it possible to mention each header file where it's needed without worrying about including it more than once.
Another person suggested presenting your #include
lines in a conventional order, with the system includes first. That is a good idea.
All of this is simply convention, rather than hard-and-fast rules. I'm offering a convention that has worked well for teams I have served on.
Edit Usually .c
and .h
files are paired. The .h
file contains the declarations necessary to use the functions in the .c
file. Sometimes that's just a function declaration, and sometimes it also includes declarations of constants, structs, and other such things. (I am writing about the C
language here. In C++
class definition rules are clearer than in C
.)
It is rare, but not impossible, that a header file you create will use stuff from a system header file. A case where it might: declaring a macro that does some kind of error logging.
Upvotes: 2