0

For public functions I'll put those in a header file such as:

// tree.h
bool TreeIsEmpty(const Tree *ptree);
bool TreeIsFull(const Tree *ptree);
size_t TreeItemCount(const Tree *ptree);

However, what about internal implementation items? Do those only go in my tree.c file or do I also put those into my tree.h file? For example, let's say for TreeItemCount I have a prototype such as:

// tree.c
static bool TreeItemCountInternal(const Tree *ptree);

Do I do anything in the header file with that?

carl.hiass
  • 1,526
  • 1
  • 6
  • 26

2 Answers2

2

static bool TreeItemCountInternal(...); has internal linkage, so you wouldn't put anything in the header file.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • I see. Even so, it's not helpful to show the full declarations of the `c` file in the header? Or is that not the way it works. – carl.hiass Mar 15 '21 at 02:29
0

If you were to declare TreeItemCountInternal without designating it as static:

bool TreeItemCountInternal(const Tree *ptree);

then you could put the forward declaration in the header file, and #include that header in you .c files. You might consider doing that if your project spans multiple .c files and you need to share a function across them. The static declaration, when applied to a function, limits it's scope to the translation unit in which it was declared (see this answer for an excellent explanation of what that means from a practical perspective).

Remember that one of the first steps your compiler takes when building a program is to take all of your #include directives, find the file in quesetion, and paste it's contents in place of the #include statement.

There is no dogmatic rule that says you should always put the declaration in the header (although it's often good practice), and the act of doing so-- in and of itself-- does not confer any particular benefit or status (but it could make a lot of difference, depending on how the rest of the program is structured).

Z4-tier
  • 7,287
  • 3
  • 26
  • 42