Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which one is right use of header file in C?

Tags:

c++

c

header

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. enter image description here

2. enter image description here

Please advice me. Thanks

like image 718
user3595632 Avatar asked Sep 28 '22 16:09

user3595632


1 Answers

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.

  • Use header-guards.
  • Only include what is needed where it is needed.
  • 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.

like image 82
alk Avatar answered Oct 05 '22 06:10

alk