3

I am still new to C. I have a question regarding source and header files. I have a header file like this:

#ifndef MISC_H_
#define MISC_H_
#define BYTE 8

#include <stdbool.h>
#include <stdio.h>
#include "DataStruct.h"

bool S_areEqual(char *firstString, char *secondString); /* (1) */
bool S_randomDataStructureCheck(char *string, DataStruct *data); /* (2) */

#endif
  1. bool is used in the function parameters, and thus, I have it in the source as well. Do I have to #include <stdbool.h> in both the header file and the source file? Are there circumstances where I would and where I wouldn't?

  2. What if I had a typedef in another header file that was used in the header as a function parameter? Do I have to #include "DataStruct.h" in both the header file and the source file?

What is the standard?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
ChrisMcJava
  • 2,145
  • 5
  • 25
  • 33
  • 1
    In your example, there is no obvious reason why the header includes ``. There is no reference to a type specified by it. You do need `` and `"DataStruct.h"`. See also [What is a good reference documenting patterns of use of header files in C?](http://stackoverflow.com/questions/256277/what-is-a-good-reference-documenting-patterns-of-use-of-h-files-in-c/256440#256440) and [Should I use `#include` in headers?](http://stackoverflow.com/questions/1804486/should-i-use-include-in-headers/1804719#1804719) – Jonathan Leffler Feb 03 '14 at 05:14
  • Yea its not obvious here, but is used during error checking in the functions themselves in the source. – ChrisMcJava Feb 03 '14 at 05:21
  • 2
    You need to distinguish between the headers that are necessary for users of the code and the headers that are necessary to implement the code. The header used by clients of the code should only include those headers that are needed by users of the code. The code that implements the functionality may well need ``, but that should be hidden in the source file, not included in the header. The header defines the interface. It should be self-contained (so it needs to include `` and `"DataStruct.h"`); it should be minimal and not contain extraneous headers (so no ``)! – Jonathan Leffler Feb 03 '14 at 05:27

2 Answers2

3

No you don't have to include in both header and source (.c) file. If you have included in a header that is included by the source then it will be available to the source as well. The order of header inclusion can be important when some headers depend on others. See this answer for more detail.

As an aside, you will notice the lines

#ifndef MISC_H_
#define MISC_H_

That ensures that the header is only included once.

Update

From the comments:

so in the source, you just include its respective header?

If you mean, should a source file only include its respective header, then it depends. Generally, files should include the files that they need.

If a source file needs a header, but that header is not needed by its own header file, then the include should go in the source rather than its header. One reason is that it is just conceptually cleaner that each file includes only the files that it needs, so it is easy to tell what the dependencies are. The other reason is that it reduces the impacts of change.

Lets look at an example. Say you have foo.c and foo.h and foo.c needs foodep.h to compile, but foo.h doesn't:

Option 1: foo.h

#include "foodep.h"

Now imagine that there are a number of other files foo1.h, foo2.h, foo3.h, etc that include foo.h. Then, any change to foodep.h affects all of those other files and their dependent header and source files.

Option 2: foo.c

#include "foodep.h"

Now, no other files have visibility of foodep.h. A change to foodep.h only impacts foo.c.

Generally, try to apply the same practices as you do with Object Oriented programming - encapsulate and minimize the scope of change.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94
  • so in the source, you just include its respective header? – ChrisMcJava Feb 03 '14 at 05:06
  • 1
    Yes; it is crucial that the source that defines the functions includes the header that declares the functions, just as it is crucial that source the uses the functions includes the header. The header ensures that the definition and uses of the functions are properly in agreement. – Jonathan Leffler Feb 03 '14 at 05:11
0

The simple way to view this is that you should always include the headers which provide functions/aliases/macros you are using in your program, whether they actually need to be included should be left to compiler.

This is because every header is defined under #ifdef - #endif clause conditioned on some header-specific MACRO (And it is necessary to do this if you define your own header, to avoid multiple inclusions and thus avoid painful compiler errors).

Thus, my advice, if you are using bool in your program, you should include stdbool.h. If the compiler has already included it in definition of some other header, it will not include stdbool again.

0xF1
  • 6,046
  • 2
  • 27
  • 50