Say I have a C program which is broken to a set of *.c and *.h files. If code from one file uses functions from another file, where should I include the header file? Inside the *.c file that used the function, or inside the header of that file?
E.g. file foo.c
includes foo.h
, which contains all declarations for foo.c
; same for bar.c
and bar.h
. Function foo1()
inside foo.c
calls bar1()
, which is declared in bar.h
and defined in bar.c
. Now the question is, should I include bar.h
inside foo.h
, or inside foo.c
?
What would be a good set of rules-of-thumb for such issues?
Structure in academic writingStart with more general and then move to the more specific ideas and points. Put more relevant/important information first. Everything is relevant to the main argument or point of the paragraph. Use cohesion to join ideas and points clearly - don't make the reader do the work.
What is the structure of an essay? The structure of an essay is divided into an introduction that presents your topic and thesis statement, a body containing your in-depth analysis and arguments, and a conclusion wrapping up your ideas.
The standard five-paragraph short essays have specific structure: introduction (1 paragraph), thesis, main body (3 paragraphs), and conclusion (1 paragraph). This helps your work be elaborately structured and easier to comprehend. First impression matters, even if you're writing a short essay.
You should include foo.h inside foo.c. This way other c files that include foo.h won't carry bar.h unnecessarily. This is my advice for including header files:
As others have noted, a header foo.h should declare the information necessary to be able to use the facilities provided by a source file foo.c. This would include the types, enumerations and functions provided by foo.c. (You don't use global variables, do you? If you do, then those are declared in foo.h too.)
The header foo.h should be self-contained and idempotent. Self-contained means that any user can include foo.h and not need to worry about which other headers may be needed (because foo.h includes those headers). Idempotent means that if the header is included more than once, there is no damage done. That is achieved by the classic technique:
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED
...rest of the contents of foo.h...
#endif /* FOO_H_INCLUDED */
The question asked:
File foo.c includes foo.h, which contains all declarations for foo.c; same for bar.c and bar.h. Function foo1() inside foo.c calls bar1(), which is declared in bar.h and defined in bar.c. Now the question is, should I include bar.h inside foo.h, or inside foo.c?
It will depend on whether the services provided by foo.h depend on bar.h or not. If other files using foo.h will need one of the types or enumerations defined by bar.h in order to use the functionality of foo.h, then foo.h should ensure that bar.h is included (by including it). However, if the services of bar.h are only used in foo.c and are not needed by those who use foo.h, then foo.h should not include bar.h
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With